Solved

How to handle IS NULL

Posted on 2011-02-11
5
240 Views
Last Modified: 2012-05-11
I need to join three site address fields together into one string. The code works fine where there a re values in each of the three address fields. However as soon as one of the three fields is blank then the record returned is blank.

I have created this code to try and say if a field is blank put a "" . However it just keeps telling me there is a syntax problem.

CASE WHEN site_address2 IS NULL THEN (' ') ELSE site_address2
0
Comment
Question by:CMChalcraft
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34872517
Did you add END ?
CASE WHEN site_address2 IS NULL THEN ' ' ELSE site_address2 END
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 34872580
use

COALESCE(Site_address2, ' ')
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 34872583
You could also just use the ISNULL(<field>, <value>) function.
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
ID: 34872778

use
coalesce(address1+',','')+coalesce(address2+',','')+coalesce(address3,'') as address


coalesce is a function which takes a list of columns/expressions and returns the first non null one from the left....


it is "equivalent" to your case statement...

case when address1+',' is null then '' else address1 end
+case when address2+',' is null then '' else address2 end
+case when address3 is null then '' else address3 end  

0
 

Author Closing Comment

by:CMChalcraft
ID: 34886204
Thanks to everyone who answered. Lowfatspread was the simplest solution. I have never heard of coalesce before.

Regards

Chris C
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Bubble user-defined Sql RAISERROR(...) to c# exception 14 225
Grid querry results 41 85
CROSS APPLY 4 69
Stored Procedure needs owner to execute 5 48
If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

736 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question