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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s