views:

234

answers:

8

hi there

I'm tryin to use SQL to build a comma separated list of cat_id's

the code is:

declare     @output varchar(max)
set         @output = null;
select @output = COALESCE(@output + ', ', '') + convert(varchar(max),cat_id)

edit: changed '' to null, STILL same. but the output im getting is like so:

, 66 , 23

the leading comma should not be there. What have i missed?

+3  A: 

Are you on SQL 2005? With props to Rob Farley who showed me this just recently:

SELECT stuff((
    SELECT ', ' + cast(cat_id as varchar(max))
    FROM categories
    FOR XML PATH('')
    ), 1, 2, '');

The inside query (with FOR XML PATH('')) selects a comma-separated list of category IDs, with a leading ", ". The outside query uses the stuff function to remove the leading comma and space.

I don't have an SQL instance handy to test this, so it's from memory. You may have to play with the stuff parameters etc to get it to work exactly how you want.

Matt Hamilton
Ok, I've tested it and it works as-is. I'd definitely recommend this over the 'append to a variable' technique.
Matt Hamilton
A: 

check @output value just before the execution of this query, I think it's not equal to NULL but to '' (empty string)

EDIT: (after the @auth edited the question)

now I'm sure it's '',

you have to initialize it to NULL

to do it independently of CONCAT_NULL_YIELDS_NULL, use the old CASE WHEN:

select @output = NULL
select @output = CASE WHEN @output IS NULL THEN '' ELSE @output+', ' END + value
najmeddine
even then `CONCAT_NULL_YIELDS_NULL` needs to be set to `ON`
The Chairman
right, thanks, I didn't know such an option existed!
najmeddine
A: 

Did you initialize @output to an empty string? COALESCE will only work if it's a NULL string.

Andomar
A: 

What you are doing wrong is that @output is not null from start, but an empty string. Set @output to null before the loop (or if it's not used since it's declared just don't assign an empty string to it).

Guffa
+2  A: 

COALESCE Returns the first nonnull expression among its arguments

First argument @output + ', ' is never null (unless you initialize @output as null AND set CONCAT_NULL_YIELDS_NULL to ON), so it's always returned.

The Chairman
+1 for the pointer to the ANSI property. Use "select sessionproperty('CONCAT_NULL_YIELDS_NULL')" to see its current value; it is 1 by default, but if it's 0, it would cause the poster's COALESCE to fail.
Andomar
A: 

And sometimes...

you have to answer your own question

declare     @output varchar(max)
select      @output = case when (@output is null) then '' else ', ' END + convert(varchar(max),cat_id)
pablo
A: 
declare     @output varchar(max)

select      @output = coalesce
                      ( 
                          @output + ', ' + convert(varchar(max),cat_id), 
                          convert(varchar(max),cat_id)
                      )
from        yourTableHere

print       @output
Michael Buen
A: 

Not sure if this applies exactly to what you're looking for, but I found this right at the same time I found your questions. I use the second solution with FOR XML PATH, which Matt Hamilton mentioned above. It's worked great for me.

Concatenating Rows - By Carl P. Anderson, 2009/10/14

http://www.sqlservercentral.com/articles/T-SQL/67973/

Tom Winter