sqlite IFNULL() in postgres

RagnarLodbrok picture RagnarLodbrok · May 12, 2017 · Viewed 40.7k times · Source

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?

Answer

Vao Tsun picture Vao Tsun · May 12, 2017

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 = ...