January 25, 2017 at 12:12 pm
Select
COALESCE (MemberHomePhoneNbr, AlternatePhone, CellPhone ) as bestphone
FROM
mhpdw2.[EDW].[MEMBER].DMEMBER DMEM
How can i modify this to always return only a tel number that is 10 in length.
Our database has some garbage tel numbers listed. I only need a valid tel #
January 25, 2017 at 12:17 pm
mw112009 - Wednesday, January 25, 2017 12:12 PM
Select
COALESCE (MemberHomePhoneNbr, AlternatePhone, CellPhone ) as bestphone
FROM
mhpdw2.[EDW].[MEMBER].DMEMBER DMEMHow can i modify this to always return only a tel number that is 10 in length.
Our database has some garbage tel numbers listed. I only need a valid tel #
No need... I figured....
Select
bestphone =
CASE
WHEN LEN(MemberHomePhoneNbr) = 10 THEN MemberHomePhoneNbr
WHEN LEN(AlternatePhone) = 10 THEN AlternatePhone
WHEN LEN(CellPhone) = 10 THEN CellPhone
ELSE
NULL
END
FROM
mhpdw2.[EDW].[MEMBER].DMEMBER DMEM
January 25, 2017 at 12:24 pm
Even better, make sure that you only have numbers.
SELECT
bestphone = CASE
WHEN MemberHomePhoneNbr LIKE REPLICATE('[0-9]',10) THEN MemberHomePhoneNbr
WHEN AlternatePhone LIKE REPLICATE('[0-9]',10) THEN AlternatePhone
WHEN CellPhone LIKE REPLICATE('[0-9]',10) THEN CellPhone
END
FROM mhpdw2.[EDW].[MEMBER].DMEMBER DMEM
January 25, 2017 at 12:30 pm
Luis Cazares - Wednesday, January 25, 2017 12:24 PMEven better, make sure that you only have numbers.
SELECT
bestphone = CASE
WHEN MemberHomePhoneNbr LIKE REPLICATE('[0-9]',10) THEN MemberHomePhoneNbr
WHEN AlternatePhone LIKE REPLICATE('[0-9]',10) THEN AlternatePhone
WHEN CellPhone LIKE REPLICATE('[0-9]',10) THEN CellPhone
END
FROM mhpdw2.[EDW].[MEMBER].DMEMBER DMEM
Not sure how the "LIKE REPLICATE" works .....BUT does the 10 gurantee that the length is 10 digits
January 25, 2017 at 2:01 pm
mw112009 - Wednesday, January 25, 2017 12:30 PMLuis Cazares - Wednesday, January 25, 2017 12:24 PMEven better, make sure that you only have numbers.
SELECT
bestphone = CASE
WHEN MemberHomePhoneNbr LIKE REPLICATE('[0-9]',10) THEN MemberHomePhoneNbr
WHEN AlternatePhone LIKE REPLICATE('[0-9]',10) THEN AlternatePhone
WHEN CellPhone LIKE REPLICATE('[0-9]',10) THEN CellPhone
END
FROM mhpdw2.[EDW].[MEMBER].DMEMBER DMEMNot sure how the "LIKE REPLICATE" works .....BUT does the 10 gurantee that the length is 10 digits
It's not hard to find out. You can either read about REPLICATE on BOL: https://msdn.microsoft.com/en-us/library/ms174383.aspx
Or you can see what happens when you use it in a SELECT.
SELECT REPLICATE('[0-9]',10)
It's basically like writing WHEN MemberHomePhoneNbr LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' THEN MemberHomePhoneNbr
Without having to count each time to remember the length.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply