Difference between revisions of "IFNULL"
From SQLZOO
(Created page with "<table align='right' border='1'> <caption>Compatibility</caption> <tr><th colspan='3'>IFNULL(f1, f2)</th></tr> <tr><td align='center'>'''Engine'''</td><td align='center'>'''OK...") |
|||
| (2 intermediate revisions by 2 users not shown) | |||
| Line 13: | Line 13: | ||
<p></p> | <p></p> | ||
<pre style='width:60ex'> | <pre style='width:60ex'> | ||
| − | + | IFNULL(x,y) = x if x is not NULL | |
IFNULL(x,y) = y if x is NULL | IFNULL(x,y) = y if x is NULL | ||
</pre> | </pre> | ||
| Line 30: | Line 30: | ||
,IFNULL(party,'None') AS aff | ,IFNULL(party,'None') AS aff | ||
FROM msp WHERE name LIKE 'C%' | FROM msp WHERE name LIKE 'C%' | ||
| + | </source> | ||
| + | <source lang='sql' class='def e-oracle'> | ||
| + | SELECT name, party | ||
| + | ,COALESCE(party,'None') AS aff | ||
| + | FROM gisq.msp WHERE name LIKE 'C%' | ||
</source> | </source> | ||
<source lang='sql' class='def'> | <source lang='sql' class='def'> | ||
| Line 43: | Line 48: | ||
<li>[[CASE |CASE function]]</li> | <li>[[CASE |CASE function]]</li> | ||
</ul> | </ul> | ||
| + | |||
| + | {{Languages}} | ||
Latest revision as of 20:24, 22 October 2012
| IFNULL(f1, f2) | ||
|---|---|---|
| Engine | OK | Alternative |
| ingres | Yes | COALESCE(f1, f2) |
| mysql | Yes | COALESCE(f1, f2) |
| oracle | No | COALESCE(f1, f2) |
| postgres | No | COALESCE(f1, f2) |
| sqlserver | No | COALESCE(f1, f2) |
IFNULL
IFNULL takes two arguments and returns the first value that is not null.
IFNULL(x,y) = x if x is not NULL IFNULL(x,y) = y if x is NULL
IFNULL can be useful when you want to replace a NULL value with some other value. In this example you show the name of the party for each MSP that has a party. For the MSP with no party (such as Canavan, Dennis) you show the string None.
SELECT name, party ,IFNULL(party,'None') AS aff FROM msp WHERE name LIKE 'C%'
SELECT name, party ,IFNULL(party,'None') AS aff FROM msp WHERE name LIKE 'C%'
SELECT name, party ,COALESCE(party,'None') AS aff FROM gisq.msp WHERE name LIKE 'C%'
SELECT name, party ,COALESCE(party,'None') AS aff FROM msp WHERE name LIKE 'C%'
See also
| Language: | English • Deutsch |
|---|