sql statement (handling NULL in concatenation)

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
LVL 1
ITsolutionWizardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nemws1Database AdministratorCommented:
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
Anthony PerkinsCommented:
Or if you are using SQL Server 2012 or 2014:
SELECT CONCAT([AltPartNo01], ' ', [AltPartNo02], ' ', [AltPartNo03]) as test 

Open in new window

0
Dany BalianCTOCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.