Conditional Statements in SQL with CASE
- If you have ever wondered how to
IFlike conditional statements in SQL,
CASEis your answer.
- It’s supported in all versions of SQL Server
CASEcan be added to both the
SELECTportion of a
SELECTstatement as well as the
CASEstatements can be used inside other
CASEyou can easily group data into various ranges, you can beautify the results of a SQL query, and can allow for dynamic sorting of the query’s results.
CASEstatements can also be used to give subtitles to
CUBEqueries, and can be used in computed columns to boot.
SELECT CASE <variable> WHEN <value> THEN <returnvalue> WHEN <othervalue> THEN <returnthis> ELSE <returndefaultcase> END AS <newcolumnname> FROM <table>
AS <newcolumnname>after the
ENDnames the resulting column. Optional.