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 @
declare @output varchar(max)
select @output = coalesce
(
@output + ', ' + convert(varchar(max),cat_id),
convert(varchar(max),cat_id)
)
from yourTableHere
print @output
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)
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