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 do I use the PostgreSQL hash function?
- How can I use PostgreSQL with YAML?
- How do I use PostgreSQL's XMIN and XMAX features?
- How can I set a PostgreSQL interval to zero?
- How can Zalando use PostgreSQL to improve its software development?
- How can I troubleshoot zero damaged pages in PostgreSQL?
- How do I use PostgreSQL ZonedDateTime to store date and time information?
- How can I extract the year from a PostgreSQL timestamp?
- How can I extract the year from a date in PostgreSQL?
- How can I retrieve data from PostgreSQL for yesterday's date?
See more codes...