[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

sql statement (handling NULL in concatenation)

Posted on 2014-08-24
4
Medium Priority
?
343 Views
Last Modified: 2014-09-11
SELECT
      [AltPartNo01] + ' ' + [AltPartNo02] + ' ' + [AltPartNo03] as test

From Table

Above statement will show test column has value if all altpartno01, 02, 03 has value. If any one of those don't have value, test shows nothing. Why and how to fix it. My goal is to make them together as one column name test.
regardless what it is inside of the column.

Thanks
0
Comment
Question by:ITsolutionWizard
3 Comments
 
LVL 23

Expert Comment

by:nemws1
ID: 40281689
When you concatenate values (+), if any of them is NULL, then the entire result will be NULL (this is the ANSI standard).  You can use either COALESCE() or ISNULL() to fix.  In this case, ISNULL() makes more sense.

SELECT
    ISNULL([AltPartNo01], '<NULL>')
    + ' '
    + ISNULL([AltPartNo02], '<NULL>')
    + ' '
    + ISNULL([AltPartNo03], '<NULL>') as test
From Table
;

Open in new window


Or if you just want empty values:
SELECT
    ISNULL([AltPartNo01], '')
    + ' '
    + ISNULL([AltPartNo02], '')
    + ' '
    + ISNULL([AltPartNo03], '') as test
From Table
;

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 40281748
Or if you are using SQL Server 2012 or 2014:
SELECT CONCAT([AltPartNo01], ' ', [AltPartNo02], ' ', [AltPartNo03]) as test 

Open in new window

0
 
LVL 11

Accepted Solution

by:
Dany Balian earned 2000 total points
ID: 40282032
as nemws1 pointed out, you can use the isnull function to get ur result
however, if you don't want spaces to be added (in case the first 2 values are null) then use the following syntax:

select isnull([altpartno01]+' ','') + isnull([altpartno02]+' ','') + isnull([altpartno03]+' ','') from table

Open in new window

0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

868 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