Solved

How to handle IS NULL

Posted on 2011-02-11
5
223 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
5 Comments
 
LVL 23

Expert Comment

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

Expert Comment

by:Paul Jackson
Comment Utility
use

COALESCE(Site_address2, ' ')
0
 
LVL 25

Expert Comment

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

Accepted Solution

by:
Lowfatspread earned 500 total points
Comment Utility

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
Comment Utility
Thanks to everyone who answered. Lowfatspread was the simplest solution. I have never heard of coalesce before.

Regards

Chris C
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now