Case statements in SQL
Case Statements in SQL :
Can be used in select,insert,update and delete .
>>Case input expression
when when_expression then resule expression
[else else_result_expression]
End
Can be used in select,insert,update and delete .
>>Case input expression
when when_expression then resule expression
[else else_result_expression]
End
SELECT EmployeeId ,EName ,Ecity ,CASE Ecity WHEN 'Delhi' THEN 'DELHI' ELSE 'NOT_DELHI' END isDelhi FROM Employees ORDER BY ,Ename ,EmployeeID;
--So in the above example we sort of created a binary variable for a categorical variable.
SELECT EmployeeId ,EName ,ESex CASE ESex WHEN 'Female' THEN '1' ELSE '0' END Coded_sex FROM Employees ORDER BY Ename ,EmployeeID;
--Seach Case Statement
SELECT trackid ,name ,bytes ,CASE WHEN BYTES < 300000 THEN 'small' WHEN BYTES >= 300001 AND BYTES <= 500000 THEN 'medium' ELSE 'large' END BYTE_SIZE FROM tracks;
Comments
Post a Comment