Add Condition in Select Clause

Standard

For example you need to build a query which displays the username and if he administrator or not according to his role. you can use case statment like the following:
note:Assume admin user has the typeID = 1

SELECT *,(SELECT CASE Type WHEN 1 THEN ‘Yes’ ELSE ‘No’ END AS IsAdmin)
From Users

Its syntax like the following:

SELECT CASE –Column Name–
WHEN –Value1– Then –Result1–
WHEN –Value2– Then –Result1–
WHEN –Value3– Then –Result1–
……..
ELSE –Default Result–
END

Advertisements