Simple CASE expression:
CASE input_expression
WHEN when_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
Searched CASE expression:
CASE
WHEN Boolean_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
SELECT CustomerID, Name,
CASE CountryCode
WHEN 'TH' THEN 'Thailand'
WHEN 'EN' THEN 'English'
WHEN 'US' THEN 'United states'
ELSE 'Unknown'
END AS Country
FROM customer
Output
CustomerID
Name
Country
C001
Win Weerachai
Thailand
C002
John Smith
English
C003
Jame Born
United states
C004
Chalee Angel
United states
นอกจากนี้ในส่วนของ THEN ยังสามารถกำหนดค่า Value ฟิวด์ Column ได้เช่นเดียวกัน
SELECT CustomerID, Name,
CASE CountryCode
WHEN 'TH' THEN Column1
WHEN 'EN' THEN 'Value'
WHEN 'US' THEN Column3
ELSE Column4
END AS Country
FROM customer