In today’s article, I will be explaining What is PostgreSQL Coalesce and how to use it.
The Coalesce function is used to replace a NULL value when querying data.
The Coalesce function is similar to the isnull() parameter in mssql, but the ISNULL() function takes two parameters, while the COALESCE() function can take up to N parameters.
EXAMPLE usage is as follows.
1 | select coalesce (NULL,1,2) |
As seen above, we wrote NULL in the first value and wrote the value 1 in the second parameter, because the coalesce works from left to right, after seeing the NULL value, it took the first value and wrote 1 to the result.
In the example above, if the vize is null in our students table, it is changed to 0.