Emel Uras Emel Uras - 6 months ago 8
SQL Question

How to return desired manual value based on specific parameters in SQL Server 2008

I have a SQL query in SQL Server 2008 like this:

declare @Waited6_8 varchar(max) = 'true'
declare @Waited8_12 varchar(max) = 'false'
declare @Waited12_18 varchar(max) = 'true'

Select
choice = case when @Waited6_8 = 'true' then '6-8'
when @Waited8_12 = 'true' then '8-12'
when @Waited12_18 = 'true' then '12-18'
end


Here, I get
6-8
as the result.

What I would like to see is:
6-8, 12-18
as one string (not as different rows)

How can I get this? I appreciate if you help.

Thanks!

Answer
declare @Waited6_8 varchar(max) = 'true'
declare @Waited8_12 varchar(max) = 'false'
declare @Waited12_18 varchar(max) = 'true'


Select choice = isnull(case when @Waited6_8 = 'true' then '6-8' end + ', ','') +
                isnull(case when @Waited8_12 = 'true' then '8-12' end + ', ','') +
                isnull(case when @Waited12_18 = 'true' then '12-18' end,'')
Comments