So I'm asked to do this query for a college project:
SELECT l.city AS name, AVG((pr.ap_price::double precision
* 7 - pr.weekly::double precision)
/((pr.ap_price::double precision))*100) AS savings_percentage
FROM locations AS l, price AS pr, apartments AS ap, hosts AS h
WHERE pr.id_apt = ap.id_apartment AND l.id_apt = ap.id_apartment AND h.host_id = ap.id_host AND h.host_identity_verified = 't'
GROUP BY l.city
ORDER BY savings_percentage DESC LIMIT 3;
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!