Wednesday, March 21, 2012

Null values in Postgresql date comparison


test=# begin;
BEGIN
test=# create temp table test(test_date date) on commit drop;
CREATE TABLE
test=# insert into test(test_date) values (null);
INSERT 0 1
test=# insert into test(test_date) values (current_date - interval '1 day');
INSERT 0 1
test=# insert into test(test_date) values (null);
INSERT 0 1
test=# select case when test_date < current_date then 'less' else 'nope' end, coalesce( test_date::varchar, 'NULL') from test;
 case |  coalesce  
------+------------
 nope | NULL
 less | 2012-03-20
 nope | NULL
(3 rows)