SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


CASE statement to include NULL value for blanks


CASE statement to include NULL value for blanks

Author
Message
SQL_ME_RICH
SQL_ME_RICH
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2262 Visits: 1596
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
Sean Lange
Sean Lange
SSC Guru
SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)

Group: General Forum Members
Points: 59105 Visits: 17946
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.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
SQL_ME_RICH
SQL_ME_RICH
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2262 Visits: 1596
Hi Sean - thanks for the reply. So just use your line in-between my final WHEN and before the ELSE?
SQL_ME_RICH
SQL_ME_RICH
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2262 Visits: 1596
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?
Sean Lange
Sean Lange
SSC Guru
SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)

Group: General Forum Members
Points: 59105 Visits: 17946
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.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
SQL_ME_RICH
SQL_ME_RICH
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2262 Visits: 1596
Awesome! Sean, thank you!

:-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search