Difference between revisions of "CASE"

From SQLZOO
Jump to: navigation, search
(Created page with "<table align='right' border='1'> <caption>Compatibility</caption> <tr><th colspan='3'>CASE WHEN b1 THEN v1 END</th></tr> <tr><td align='center'>'''Engine'''</td><td align='cen...")
 
 
(3 intermediate revisions by one user not shown)
Line 9: Line 9:
 
<tr><td align='left'>sqlserver</td><td>Yes</td><td></td></tr>
 
<tr><td align='left'>sqlserver</td><td>Yes</td><td></td></tr>
 
</table>
 
</table>
<h3>CASE</h3>
+
<h1>CASE</h1>
 
<p>CASE allows you to return different values under different conditions. </p>
 
<p>CASE allows you to return different values under different conditions. </p>
 
<p>If there no conditions match (and there is not ELSE) then NULL is returned.</p>  
 
<p>If there no conditions match (and there is not ELSE) then NULL is returned.</p>  
Line 22: Line 22:
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
SELECT name, population
 
SELECT name, population
       ,CASE WHEN population<1000000 THEN 'small'
+
       ,CASE WHEN population<1000000  
             WHEN population<10000000 THEN 'medium'
+
            THEN 'small'
 +
             WHEN population<10000000  
 +
            THEN 'medium'
 
             ELSE 'large'
 
             ELSE 'large'
 
       END
 
       END
Line 32: Line 34:
 
<p>See also</p>
 
<p>See also</p>
 
<ul>
 
<ul>
   <li>[[NVL function]]</li>
+
   <li>[[NVL |NVL function]]</li>
 
</ul>
 
</ul>
 +
 +
{{Languages}}

Latest revision as of 09:29, 1 October 2012

Compatibility
CASE WHEN b1 THEN v1 END
EngineOKAlternative
ingresYes
mysqlYes
oracleYes
postgresYes
sqlserverYes

CASE

CASE allows you to return different values under different conditions.

If there no conditions match (and there is not ELSE) then NULL is returned.

  CASE WHEN condition1 THEN value1 
       WHEN condition2 THEN value2  
       ELSE def_value 
  END 
SELECT name, population
      ,CASE WHEN population<1000000 
            THEN 'small'
            WHEN population<10000000 
            THEN 'medium'
            ELSE 'large'
       END
  FROM bbc

See also

Language: English  • Deutsch
Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense