sql - Fill Variable with Case Statement Results -


i have questionnaire users have filled out (several thousand day)

the result each questionnaire record contains 70 fields (that correspond each question)

i've been asked identify affirmatives each of 70 questions , concatentate them 1 field (a summary of issues identified record).

in other languages (vba in particular) accomlish initializing variable '', looping through recordset , setting variable + field name of issue. i'm not sure how accomplish in sql.

i've tried...

declare @strfyi nvarchar set @strfyi = ''  select     a.record_num     ,case          when a.date_missing = 'yes' @strfyi = @strfyi + 'date_missing, '          when a.unclear_images = 'yes' @strfyi = @strfyi + 'unclear_images, '          when a.damage = 'yes' @strfyi = @strfyi + 'damage, '          else @strfyi     end fyi_reasons     questionaretable 

but doesn't work. i'll need trim last comma , space off list once it's generated, shouldn't problem... i'm not sure how iterate through records , build concatenation in tsql :) i'm not sure (because syntax wrong) if variable reset '' before each record evaluated!

can me out here?

this ugly 70 columns.

select record_num, left(strfyi, len(strfyi) - 2)  (     select         a.record_num,         (case when a.date_missing = 'yes' 'date_missing, ' else '' end) +          (case when a.unclear_images = 'yes' 'unclear_images, ' else  '' end) +          (case when a.damage = 'yes' 'damage, ' else '' end)  strfyi             questionaretable      ) t 

maybe cleaner using iif

iif ( boolean_expression, true_value, false_value )

select record_num, left(strfyi, len(strfyi) - 2)  (     select         a.record_num,         iif(a.date_missing = 'yes', 'date_missing, ' , '' ) +          iif(a.unclear_images = 'yes', 'unclear_images, ',  '') +          iif(a.damage = 'yes', 'damage, ', '')  strfyi             questionaretable      ) t 

as celliot mention not iif in 2008 solution may be

isnull((select 'date_missing, ' a.date_missing = 'yes'),'') 

Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -