How to use Switch-Case in Select SQL Server Query

Some times we need to apply switch case in sql query when we retrieved the data. At that time this blog may help you.

let's assume a table is given as follow:

username password roleid
aaa          sss             1
bbb         fff               1
ccc          gdfg           2
ddd         fsdfsd         2

we want the data is such manner

1=admin
2=client

username password roleid
aaa           sss           admin
bbb          fff             admin
ccc           gdfg         client
ddd          fsdfsd       client

Then we can write query like this:

select CASE RoleID WHEN 1 THEN 'Admin' ELSE 'Client' END as RoleID from tbl_User