SQL CASE expression syntax
SQL CASE expressions, also known as "CASE WHEN statements", allow you to evaluate multiple conditions and return different values based on the results. Its syntax varies depending on the database engine used.
SQL Server syntax:
<code class="language-sql">CASE case-expression WHEN when-expression-1 THEN value-1 [ WHEN when-expression-n THEN value-n ... ] [ ELSE else-value ] END</code>
<code class="language-sql">CASE WHEN boolean-when-expression-1 THEN value-1 [ WHEN boolean-when-expression-n THEN value-n ... ] [ ELSE else-value ] END</code>
Among them:
Note that the order of the WHEN clauses is important and the first match is used. If no ELSE clause is provided and no matching WHEN condition is met, the result will be NULL.
The above is the detailed content of How Does the SQL CASE Expression Work and What is its Syntax?. For more information, please follow other related articles on the PHP Chinese website!