ERROR: invalid input syntax for type numeric (SQL) - sql

I've created a data set with Mockaroo and then added the file with cmd. But I am getting this error.
ERROR: invalid input syntax for type numeric:
The table definiton is this :
create table car(
id BIGSERIAL NOT NULL PRIMARY KEY,
make VARCHAR(100) NOT NULL,
model VARCHAR(100) NOT NULL,
price NUMERIC(19,2) NOT NULL);
And this is how the numeric number (price) looks like:
insert into car (id, make, model, price) values (1, 'Mercury', 'Grand Marquis', '$52656.16');
I added the cmd screen shot for you to see.
Note : the translated version of the error written in my cmd is:
ERROR: invalid input syntax for type numeric:
p.s : I tried making (19,2) to even (50,20). It did not work.
cmd

Your price column is numeric and data that you are entering into it is varchar('$52656.16')
Either replace '$52656.16' with numeric value 52656.16 or change price column to varchar.

If you use VARCHAR for the price column you may still get a invalid input syntax for type numeric error.
What i think you should try is changing the data sets from mockaroo by changing the money option with numbers and add the decimals (you can choose 2 decimals option) option. You should have a data set without the $ sign.

Related

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.

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

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;

Conversion failed when converting the varchar value '0%' to data type int

I'm facing a problem with SQL Server.
I've created a table like this:
create table Components
(
pk BIGINT NOT NULL PRIMARY KEY IDENTITY,
id VARCHAR(50),
descr VARCHAR(50),
in_m INT,
in_iy INT,
p_fw VARCHAR(5000)
);
and I'm trying to insert a couple of values:
insert into Components (id, descr, in_m, in_iy, p_fw)
values ('FW000_A', '0%', 0, 0, '[0.0,0.0,0.0]'),
('FW000_B', '1%', 1, 1, '[1.0,1.0,1.0]');
I get the following error:
Msg 245, Level 16, State 1, Line 111
Conversion failed when converting the varchar value '0%' to data type int.
even though the column descr is correctly defined as varchar(50).
Can anybody help me please? Why is SQL Server trying to convert my strings to int values?
What's missing from your question is that you have more than just the two values lines you've shown, and one of the other ones has an integer literal for the descr column, rather than a string.
This example produces the same error:
declare #t table (Descr varchar(50) not null)
insert into #t(Descr) values
('0%'),
(12)
What I believe happens is that SQL Server first tries to determine the data types for all columns in the values clause. Using data type precedence rules, it observes a varchar literal in one row and an int literal in the other, and so determines that the overall type for this column is int and attempts to perform the conversion that leads to the error.
During this process, it does not use any information about the target table into which the values are going to be placed, including the data types of the columns there.
run this and verify the data types;
sp_columns Components
Looks like 'descr' is really an integer

Can't convert postgresql table column from type varchar to int

I have a database table of that I have used to store the data returned from a web spider. I have a column that contains ticket prices for different events all in the varchar type (as the scrapy spider has to scrape the data in unicode). I'm trying to return the min price of the column and since the min() function only works for data of type INT, I tried to convert the column to integers using a solution from this SO post:
ALTER TABLE vs_tickets ALTER COLUMN ticketprice TYPE integer USING (ticketprice::integer);
but I got the error: ERROR: invalid input syntax for integer:
I also tried: change_column :vs_tickets, :ticketprice, 'integer USING CAST(ticketprice AS integer)' but that didn't work either.
What is the proper way to convert the column to type INT?
Edit:
You have decimal places in the string, so a simple cast is not going to work. You can do a double conversion:
cast(cast(ticketprice as decimal(10, 2)) as int)
or:
(ticketprice::decimal(10, 2))::int
(The parens are not strictly necessary.)
EDIT:
Or, as Erwin points out, just use numeric:
(ticketprice::numeric)::int
Postgres is much smarter about numeric than most other databases . . . after all, it supports numbers that are egregiously large ;)
The final query is:
ALTER TABLE vs_tickets
ALTER COLUMN ticketprice TYPE integer USING (ticketprice::numeric::integer);
I'm going to bet on your column have wrong characters.
Also you may want use float or numeric because you will lose decimals if convert to integers.
You need create a function to check if a text is numeric like this isnumeric-with-postgresql
Then check each row like this
select ticketprice
from vs_tickets
where ISNUMERIC(ticketprice) = false;
As your comment you also should try
SELECT ticketprice::float
You will be best off adding an INT column, moving your data with a cast and then removing the old varchar column.
ALTER TABLE vs_tickets ADD COLUMN ticketprice_int TYPE int;
GO
update vs_tickets SET ticketprice_int = cast(ticketprice as int);
// if you fail to cast the varchar to int you can use Gordon's method
// update vs_tickets SET ticketprice_int = cast(cast(ticketprice as decimal(10, 2)) as int);
GO
ALTER TABLE vs_tickets DROP COLUMN ticketprice;
GO
ALTER TABLE vs_tickets RENAME COLUMN ticketprice_int to ticketprice;
GO
With this at minimum you will be able to tell if and where a cast/convert fails and be able to check and recheck at each step before you can't turn back.