Thursday, November 7, 2013

[SQL] Case Expression: Example to combine two columns into one column within a table if a field is empty (with Image)

SELECT brand_id, brand_name_en, brand_name_tc
FROM db2_prod.tbl_brand
By using the script can get the data shown below:


And my duty is to conbine 2 columns into one column, 
the condition is there is if no any value in field "brand_name_tc", show the value in "brand_name_en".
(If there is any Chinese brand name, show chinese name; if no chinese name, show english barnd name)

And finally i have used "CASE expression":

SELECT brand_id,
Case when brand_name_tc=' ' then brand_name_en
when brand_name_tc !=' ' then brand_name_tc
else 'nothing'
End as brand_name
FROM db2_prod.tbl_brand;

And get

No comments :

Post a Comment