What is the equivalent of SQLite's IFNULL()
in Postgres?
I have to following query (sqlite in Ruby):
SELECT ifnull(max(code_id) + 1, 1) FROM configentries WHERE configtable_id = ...
How should this look like if I want the same result with PostgreSQL?
SQL Server supports ISNULL function that replaces NULL with a specified replacement value: ISNULL(expression, replacement) If the expression is NULL, then the ISNULL function returns the replacement . Otherwise, it returns the result of the expression . PostgreSQL does not have the ISNULL function.
The ifnull function can be used in the following versions of SQLite: SQLite 3.8. 6, SQLite 3.8. x, SQLite 3.7.
SELECT * FROM employees WHERE first_number IS NULL; This PostgreSQL IS NULL example will return all records from the employees table where the first_name contains a NULL value.
try coalesce
:
The COALESCE function returns the first of its arguments that is not null. Null is returned only if all arguments are null
SELECT coalesce(max(code_id) + 1, 1) FROM configentries WHERE configtable_id = ...
Try this, Select NULLIF(Max(code_id), 0) +1 from configentries WHERE configtable_id = ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With