Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlite IFNULL() in postgres

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?

like image 743
RagnarLodbrok Avatar asked May 12 '17 09:05

RagnarLodbrok


People also ask

What is Ifnull in 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.

Does SQLite support Isnull?

The ifnull function can be used in the following versions of SQLite: SQLite 3.8. 6, SQLite 3.8. x, SQLite 3.7.

How do I check for NULL in PostgreSQL?

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.


2 Answers

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 = ... 
like image 65
Vao Tsun Avatar answered Sep 27 '22 19:09

Vao Tsun


Try this, Select NULLIF(Max(code_id), 0) +1 from configentries WHERE configtable_id = ...

like image 21
rurugg Avatar answered Sep 27 '22 17:09

rurugg