June 14, 2011 at 9:56 am
Hello -
I have a section of code that is part of a PROC, where the conditions being looked at is using a CASE statement, along with SUBSTRING conditions. The need that I have to account for is if a blank value were to be passed through, it needs to be treated as a NULL, and I am not sure how to account for this in the current construct. Here are a couple of examples of the current code...
Secondary Area Code
CASE LEFT(PhoneAlt,1)
WHEN '(' THEN SUBSTRING(PhoneAlt,2,3)
ELSE LTRIM(LEFT(PhoneAlt,3))
END As [Provider!2!SecondaryAreaCode!element]
Secondary Phone Number
CASE
WHEN SUBSTRING(PhoneAlt,5,1) = ')' THEN SUBSTRING(PhoneAlt,6,8)
WHEN SUBSTRING(PhoneAlt,4,1) = '-' THEN SUBSTRING(PhoneAlt,5,8)
ELSE LTRIM(SUBSTRING(PhoneAlt,4,8))
END As [Provider!2!SecondaryPhone!element]
Any ideas on how I would set an additional condition to account for a blank value to be accounted for as a NULL?
Many thanks in advance
June 14, 2011 at 10:11 am
nullif([your column here], '''')
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
June 14, 2011 at 10:16 am
Hi Sean - thanks for the reply. So just use your line in-between my final WHEN and before the ELSE?
June 14, 2011 at 10:22 am
OK - what if I need to make certain that there isn't just 1 space, but say 2 or 3 or 10 spaces? How would I incorporate LTRIM and RTRIM with your example?
June 14, 2011 at 10:30 am
Rich Yarger (6/14/2011)
OK - what if I need to make certain that there isn't just 1 space, but say 2 or 3 or 10 spaces? How would I incorporate LTRIM and RTRIM with your example?
like this
nullif(CASE LEFT(PhoneAlt,1)
WHEN '(' THEN SUBSTRING(PhoneAlt,2,3)
ELSE LTRIM(LEFT(PhoneAlt,3))
END, '') As [Provider!2!SecondaryAreaCode!element]
Remember that string comparisons in sql will trim unless it for comparisons.
Look at the following:
declare @varchar varchar(10), @char char(10)
set @varchar = 'a'
set @char = 'a'
select DATALENGTH(@varchar), DATALENGTH(@Char)
where @varchar = @char
Here you can see that the actual data is not the same but when comparing they are equal. 😉
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
June 14, 2011 at 10:32 am
Awesome! Sean, thank you!
🙂
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply