Skip to content

Instantly share code, notes, and snippets.

@zelark
Last active August 29, 2015 14:19
Show Gist options
  • Save zelark/c12de2e7e07310861d07 to your computer and use it in GitHub Desktop.
Save zelark/c12de2e7e07310861d07 to your computer and use it in GitHub Desktop.
ERROR: invalid input syntax for type numeric: ""
create table test
(
name character varying(20),
id character varying(20) not null
);
insert into test (name, id) values ('bar', '');
insert into test (name, id) values ('foo', '11');
insert into test (name, id) values ('foo', '22');
-- This query is OK
select * from test where id in (11, 22) and name = 'foo';
-- This query is wrong
select * from test where id in (11, 22);
-- and how to solve this problem
create or replace function public.to_number(t varchar)
returns numeric
as
$BODY$
select case when t = '' then null else t end::text::numeric;
$BODY$
language sql immutable strict;
create cast (character varying as numeric) with function to_number(varchar) as implicit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment