sql server - SQL CASE LIKE with multiple choices -
in query have following part of code:
case when field1 '%choice1%' or field1 '%choice1%' .... 'category 1' when field1 '%choicea%' or field1 '%choiceb%' .... 'category 2' end 'cats'
i have numerous of choices (1line 20), (2line 15 , son on).
can make code cleaner, use in
for?
example:
case when field1 in ('%choice1'.'%choice2%',...) 'category 1' when field1 in ('%choicea'.'%choiceb%',...) 'category 2' end 'cats'
p.s. heard can use somehow ||
can't find in google how use them.
what think problem?
if choices known, put them in table , use join:
select field1, c.category maintable left join choices c on field1 ('%' + c.choice + '%')
update:
if there sub-strings inside 1 category, add distinct
query.
Comments
Post a Comment