Error: invalid input syntax for type numeric: "" (postgresql) - sql

I'm trying to alter the column data type in my postgresql table. The column name is _2010_10, type is text, and the value is 18.74 (in text format). I'm trying to change the text type to numeric. This is my input/output:
ALTER table cadata.pricetorentratio
ALTER column _2010_10 type numeric USING (trim(_2010_10)::numeric);
ERROR: invalid input syntax for type numeric: ""
Not sure why I'm getting this error.

You could use NULLIF to handle blank string '':
ALTER table pricetorentratio
ALTER column _2010_10 type numeric USING (NULLIF(trim(_2010_10),'')::numeric);
DBFiddle Demo
SELECT ''::numeric
-- invalid input syntax for type numeric: ""

ALTER TABLE `pricetorentratio` CHANGE `_2010_10` `_2010_10` FLOAT NULL DEFAULT NULL;

Related

ERROR: invalid input syntax for type numeric: ""

ALTER TABLE unicorns
ALTER COLUMN last_valuation_upd TYPE numeric USING (last_valuation_upd::numeric);
It shows this error:
ERROR: invalid input syntax for type numeric: "" SQL state: 22P02
I am assuming you had the column as text before and now changing it to numeric. Probably the quotes and commas (if any) existing in the table can be a problem and may need to be removed.

convert varchar[ ] column type to uuid[ ]

there is a column of type character varying[] , which contains data of type uuid like
{0f071799-e37e-4e1c-9620-e580447416fe,913a7134-6092-45fa-ae18-163302db8112},
but there are also some old values of another type like {5edfd4edfa1bb21a142442a0}.
How can the column type be converted?
I used the script:
alter table services alter column office_ids type uuid[] USING office_ids::uuid[];
but gives an error - invalid syntax for type uuid: "5edfd4edfa1bb21a142442a0".
You must first convert your 25 character values into valid uuid values.
One such conversion would be:
8f5f7cc46821423fa6057025a -> 00000008-f5f7-cc46-8214-23fa6057025a
The SQL for this is:
regexp_replace('8f5f7cc46821423fa6057025a', '^(.)(.{4})(.{4})(.{4})(.{12})^', '0000000\1-\2-\3-\4-\5')
output:
00000008-f5f7-cc46-8214-23fa6057025a
Which leaves valid uuids unchanged. See live demo.
You can use this to update the bad values like this:
update services set office_ids = array(
select regexp_replace(t.val, '^(.)(.{4})(.{4})(.{4})(.{12})$', '0000000\1-\2-\3-\4-\5')
from unnest(services.office_ids) as t(val)
)
Then your alter command will work.
See live demo.

postgresql changing column type from array to integer throwing casting error

I am changing the postgresql column data type from integer[] to integer, while executing below query,
alter table contact_type alter column is_delete set data type integer USING is_delete::integer
i am getting below error
ERROR: cannot cast type integer[] to integer
LINE 1: ...umn is_delete set data type integer USING is_delete::integer
^
SQL state: 42846
Character: 86
but when tried to change datatype from varchar[] to char, below query works fine
alter table contact_type alter column ct_type set data type varchar
i have referred this link so link but it is not working for converting array to normal data type..
Edit :- it is empty table without any data...
You need to pick the array element that you want to use. You can't convert e.g. 42 integers to a single one.
E.g. if you want to use the first element of the array:
alter table contact_type
alter column is_delete
set data type integer USING is_delete[1];
But a column named is_delete should probably be a boolean rather than an integer.

Why can't I modify column type in Postgres

I am trying to change type of columns from varchar to integer. When I run the following command
alter table audio_session
alter column module_type_cd type INT USING module_type_cd::integer,
alter column sound_type_cd type INT USING sound_type_cd::integer
I got the error:
ERROR: invalid input syntax for integer: "string"
SQL state: 22P02
The error does not make any sense. There is no "string" in the command at all. What did I do wrong?
The columns audio_session.module_type_cd or audio_session.sound_type_cd has at least one non-numeric value among all values of each row. So, it's impossible to convert to a completely numeric data type.
As an example :
create table Table1( col1 varchar(10), col2 varchar(10) );
insert into Table1 values('1','1');
insert into Table1 values('2','2');
insert into Table1 values('3','C3');
select CAST(coalesce(col1, '0') AS integer) as converted from Table1; -- this works
select CAST(coalesce(col2, '0') AS integer) as converted from Table1; -- but, this doesn't
SQL Fiddle Demo
There is no "string" in the command at all.
But must be in the data. The command you're going to use tries to cast all the columns values to integer. Check this:
select *
from audio_session
where module_type_cd = 'string' or sound_type_cd = 'string';

Postgresql how to change numrange to numeric column type

ALTER TABLE tb_economic_parameter
ALTER COLUMN c_economic_parameter_value TYPE numeric
USING c_economic_parameter_value::numeric ;
I get this error:
Query failed: ERROR: cannot cast type numrange to numeric
How can I change it?