Skip to content

Instantly share code, notes, and snippets.

@mwotton
Created December 17, 2018 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwotton/3595256425790339822ecdb8084c5c1d to your computer and use it in GitHub Desktop.
Save mwotton/3595256425790339822ecdb8084c5c1d to your computer and use it in GitHub Desktop.
create function polycast(input text, typ anyelement) returns anyelement language plpgsql immutable as
$$
begin
return input::anyelement;
end;
$$;
create function nullable(input text, typ anyelement) returns anyelement language plpgsql immutable as
$$
begin
return polycast(input, typ);
exception
when others then
return typ;
end;$$;
postgres=# select 1 where nullable('abc', null::timestamptz) < nullable('2018-01-01 00:00:00', null::timestamptz);
?column?
----------
(0 rows)
postgres=# select 1 where nullable('2018-01-01 00:00:00', null::timestamptz) < nullable('2018-01-01 10:00:00', null::timestamptz);
?column?
----------
1
(1 row)
postgres=# select 1 where nullable('2018', null::numeric) < nullable('2019', null::numeric);
?column?
----------
1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment