Solved

How to handle IS NULL

Posted on 2011-02-11
5
232 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 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