postgresqlHow do I use the PostgreSQL COALESCE function?
The PostgreSQL COALESCE
function is used to return the first non-null value from a list of values. It takes in any number of arguments and returns the first one that is not NULL
.
Example
SELECT COALESCE(NULL, 5, 0);
Output example
5
The code above will return the value 5
because it is the first non-null value in the list.
The parts of the code are:
SELECT
: This is the keyword used to specify that a query is being made.COALESCE
: This is the function being used to return the first non-null value.NULL
: This is a value that is being checked for.5
: This is the value that is returned because it is the first non-null value.0
: This is the last value in the list, but it is not returned because it is not the first non-null value.
For more information on the COALESCE
function, please see the following link:
More of Postgresql
- How can I write a PostgreSQL query to retrieve JSON data?
- How can I use PostgreSQL and ZFS snapshots together?
- How do I set the PostgreSQL work_mem parameter?
- How can I troubleshoot zero damaged pages in PostgreSQL?
- How can I set a PostgreSQL interval to zero?
- How do I use PostgreSQL's XMIN and XMAX features?
- How can I use PostgreSQL's "zero if null" feature?
- How do I use PostgreSQL ZonedDateTime to store date and time information?
- How can I integrate PostgreSQL with Yii2?
- How can I use PostgreSQL with YAML?
See more codes...