Skip to content
Advertisement

PostgreSQL Can’t Convert String to double precision

So I’m asked to do this query for a college project:

Where pr.ap_price and pr.ap_weekly are both saved as strings (VARCHAR(255)), and I’ve tried to cast them as other types wether using CAST(pr.ap_price AS double precision) or using the option found in the code above. It always ends up showing the same error wether I cast it to numeric or double precision: ERROR: la sintaxis de entrada no es válida para tipo double precision: «1,100.00» SQL state: 22P02 (In Spanish but basically says ERROR: the entrance syntaxis isn’t valid for type double precision)

How can I cast it correctly? My objective is to turn it from string to numbers to be able to compare them.

I have also tried using: ALTER TABLE apartments_importacio ALTER COLUMN price TYPE double precision USING (price::double precision); but the same error shows. (Where apartments_importacio is the table where I copy the .csv file to later insert into the other tables)

Any clues? Thanks in advance!

Advertisement

Answer

I’m going to say it is because of this:

The reason being an empty string is not a valid number.

One solution is to do:

Otherwise you will need to go through an clean up the empty strings in those columns and change them to 0 or NULL. Long term I would say making those field float8 or better yet numeric is the better solution. Then you deal with the issue on creating the record.

UPDATE. Dealing with number formatting:

The solution to above is:

For more information see

https://www.postgresql.org/docs/current/functions-formatting.html

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement