0

I have the following SQL query (SQL Server 2005):

Select TOP 1000 col1,
LTRIM(RTRIM(cast(col2 as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(col3 as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(col4 as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(col5 as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(col6 as varchar))))
from mytable

But I'm having some problems because these columns are all nullable and I cannot have NULL values in this concatenation. What's the nicest way to avoid NULL values in this situation?

Thanks in advance!

4 Answers 4

3

Wrap the fields in ISNULL()

Select TOP 1000 col1,
LTRIM(RTRIM(cast(ISNULL(col2,'') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(ISNULL(col3,'') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(ISNULL(col4,'') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(ISNULL(col5,'') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(ISNULL(col6,'') as varchar))))
from mytable

Or COALESCE()

Select TOP 1000 col1,
LTRIM(RTRIM(cast(COALESCE(col2,'') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(COALESCE(col3,'') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(COALESCE(col4,'') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(COALESCE(col5,'') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(COALESCE(col6,'') as varchar))))
from mytable

If using SQL Server 2012 it's even easier thanks to CONCAT() (you can replace SPACE(2) with ' | ' above too):

Select TOP 1000 col1
          ,CONCAT(LTRIM(RTRIM(cast(col2 as varchar)))
                ,' | ', LTRIM(RTRIM(col3)) 
                ,' | ', LTRIM(RTRIM(col4)) 
                ,' | ', LTRIM(RTRIM(col5)) 
                ,' | ', LTRIM(RTRIM(col6)))
from mytable
Sign up to request clarification or add additional context in comments.

6 Comments

How would the query look like if I use COALESCE()?
just replace ISNULL with COALESCE :-)
:-) haha thanks. I didn't know the syntax was EXACTLY the same... :-)
it's COALESCE not COALEASCE
Typo's abound in the morning!
|
1

You can have some other value when there is a NUll, through ISNULL function. Here is a sample query:

Select TOP 1000 col1,
LTRIM(RTRIM(cast(ISNULL(col2,'SOMEVALUE') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(ISNULL(col3,'SOMEVALUE') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(ISNULL(col4,'SOMEVALUE') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(ISNULL(col5,'SOMEVALUE') as varchar))) 
+ SPACE(2) + '|' + SPACE(2) + LTRIM(RTRIM(cast(ISNULL(col6,'SOMEVALUE') as varchar))))
from mytable

Comments

1

If this is a non-critical system and you can't be bothered to COALESCE or ISNULL every column, you could use the CONCAT_NULL_YIELDS_NULL server variable which treats concatenating null values as empty strings:

SELECT 'Hi ' + NULL

Would yield null.

SET CONCAT_NULL_YIELDS_NULL OFF

SELECT 'Hi' + NULL

SET CONCAT_NULL_YIELDS_NULL ON

Produces: "Hi".

Documentation is here - be aware that they are going to remove this feature in future however.

Otherwise, just turn the setting off, and run your original query.

http://technet.microsoft.com/en-us/library/ms176056.aspx

5 Comments

Not sure why you got downvoted, your answer is consistent with what I've read. In 2012 they've changed this, and now CONCAT() can be used, but since OP didn't specify version your answer seems fine to me.
Why suggest a method that's been deprecated?
@gvee As I said: If this is a non-critical system and you can't be bothered to COALESCE or ISNULL every column. It might be this is a quick fix for something, and so this is less work.
The note marked "Important" in the documentation kind of answers it all. I don't care if it's non-critical; it's really bad practice. When it's removed from a future version would the solution then be "set the compatibility of your database to 100 then use this option"?
@gvee No of course not - if this admittedly temporary solution were migrated further than SQL Server 2012, it would be better to use one of the other answers here - e.g. CONCAT() from Goat's answer above (I gave that answer an upvote).
0

If you want to properly concatenate your values and don't want to use some placeholder for nulls, use isnull (or coalesce) + stuff:

select top 1000
    col1,
    stuff(
       isnull('  |  ' + cast(col2 as varchar), '') +
       isnull('  |  ' + cast(col3 as varchar), '') +
       isnull('  |  ' + cast(col4 as varchar), '') +
       isnull('  |  ' + cast(col5 as varchar), '')
    , 1, 3, '')
from mytable

sql fiddle demo

2 Comments

Not sure why the downvote, if looking for a consistent number of delimiters then obviously this wouldn't do, but OP doesn't specify, so this answer adds value.
@GoatCO Actually in my expirience it's harder to come up with solution with insonsistent number of delimiters. It's very easy just to put isnull(.., smth), but this one is readable solution for proper concatenation with nulls. Thanks for upvote if it's you :) Let's wait fo OP, for me it's looking that he needs my solution :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.