SQL coalesce
takes two or more arguments and returns the first value that is not null
or the SQL null
value if all arguments are null
.
SELECT COALESCE(first, second, 'default')
FROM …
The example returns the value of first
if it is not null
. Otherwise it returns the value of second
if that is not null
. Otherwise it returns the text default
.
Tutorials
Other conditional expressions:
Product specific features: nvl
, nvl2
, ifnull
, isnull
, if
, decode
.
The coalesce
expression is defined in ISO/IEC 9075-2:2023 as mandatory F261-04.
You can’t catch up on 20 years of SQL evolution in one day. Subscribe the newsletter via E-Mail, Twitter or RSS to gradually catch up and to keep modern-sql.com on your radar.
The essence of SQL tuning in 200 pages
Buy now!
(paperback and/or PDF)
Paperback also available at Amazon.com.
Markus offers SQL training and consulting for developers working at companies of all sizes.
Learn more »