concatenation - sql - concatenating case statements -


i want similar to:

select    ('['+twt.dept+']' +       case when twt.typ <> 'empty' , twt.typ > '' '-['+twt.typ+']' end      + case when twt.subtyp_1 <> 'empty' , twt.subtyp_1 > '' '-['+twt.subtyp_1+']' end      + case when twt.subtyp_2 <> 'empty' , twt.subtyp_2 > '' '-['+twt.subtyp_2+']' end      + case when twt.subtyp_3 <> 'empty' , twt.subtyp_3 > '' '-['+twt.subtyp_3+']' end) category table1 tb1 join table2 twt on (tb1.id = twt.id) 

i know can use coalesce or using stacking cases, anyways use simpler looking syntax have here achieve (especially without having monster case statement 5th case)?

ps category coming empty, while have verified atleast 2 of field above contain values.

i hoping [shoes]-[sandals]-[pancakes]-[cinnamon bun]

i don't think can make query shorter. use separate fields. technically use functions case when work, don't think gain enough.

create function checkandadd(@str varchar(255)) returns varchar(255) begin     return case when @str <> 'empty' , @str <> '' '-['+@str+']' else '' end end 

note if case fails , doesn't have else returns null, , in sql 'hello' + null = null

simple test:

select 'hello ' + case when 1 = 0 'none' end + ' world' 

correct test:

select 'hello ' + case when 1 = 0 'none' else '' end + ' world' 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -