question

kpolomon avatar image
kpolomon asked

sql case statement

I have one column with different values and would like to group them into 3 variables in a new column. For example, column A has (FG01, FN02, G09, FG12, FN30, G15). I would like to create a new column and in it I would want to see the following - FG, FN and G. Below is the case statement I have and it is yielding only two i.e. G and FN. 'GNMA/FHLMC' = CASE WHEN a.cCode LIKE '%G%' THEN 'G' WHEN a.cCode LIKE '%FG%' THEN 'FH' ELSE 'FN' END
likecase-statementcasecase-expression
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

· Write an Answer
Tom Staab avatar image
Tom Staab answered
You need to change the order of options because %G% includes %FG%. Try this: CASE WHEN a.cCode LIKE '%FG%' THEN 'FG' WHEN a.cCode LIKE '%G%' THEN 'G' ELSE 'FN' END
1 comment
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Thank you so much. It worked. Perfect!!
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.