How to use case in query statement in SQLServer.
การใช้ CASE ใน select statement บน sql server
# ถ้าเราต้องการเปรียบกับค่าใดค่าหนึ่ง
select c1, c2, var = case c3
when 1 then 'hell one'
when 2 then 'hello two'
when 3 then 'hello three'
else 'hello unknown'
end
from table
# ถ้าเราต้องการเปรียบกับค่าที่เป็นช่วง ซึ่งเราจะไม่ใส่ c3 ต่อ case
select c1, c2, var = case
when (c3 >= 1.0 and c3 <= 2.0) then 'hello'
else 'hello else'
end
from table
Reference: craigmullins.com, daniweb.com
No comments:
Post a Comment