"invalid input syntax for integer" Is Reported
Symptom
The type of a table column is varchar(20), and the data is 5.0. When cast(xxx as integer) is used to convert the data to an integer, an error is reported. The error information is as follows:

Possible Causes
The varchar data type cannot be directly converted to the integer type. You can change the column type to decimal (any precision), then perform conversion.
Handling Procedure
- Reproduce the fault. Assuming that the name of the error table is product, define the table as follows:
1SELECT * FROM PG_GET_TABLEDEF('product');

- Run the following SQL statement and convert the query result to an integer. The error is reported as follows:
1SELECT CAST(price AS integer) FROM product;

- Change the data type of the column to decimal.
1ALTER TABLE product ALTER COLUMN price TYPE decimal(10,1);
- Retry the query again. The integer conversion is successful.
1SELECT CAST(price AS integer) FROM product;

Last Article: Execution Plan Scan Hints Do Not Take Effect
Next Article: Error UNION types %s and %s cannot be matched Is Reported
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.