Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 325
Emel Uras Avatar asked Nov 28 '25 14:11

Emel Uras


1 Answers

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,'')
like image 194
S3S Avatar answered Dec 01 '25 11:12

S3S