Last active
August 29, 2015 14:19
-
-
Save zelark/c12de2e7e07310861d07 to your computer and use it in GitHub Desktop.
ERROR: invalid input syntax for type numeric: ""
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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