2

I have two columns of latitude and longitude in a table with the column datatypes as string. There also some entries which are 'blank' but I think saved as a "" string.

I have had a look through the some of the similar questions here but I can't find an exact answer.

When I try to alter the column datatype I get the following error.

ALTER TABLE listings ALTER COLUMN latitude TYPE FLOAT USING latitude::float;                                                                                                                                
ERROR:  invalid input syntax for type double precision: ""

I think this is because the blanks/nulls are being considered as a string "".

Whats the best way to deal with this?

Thanks

1
  • 1
    No it is because there are empty strings in the fields. If they where NULL's then you would not be seeing the problem. Other databases may consider '' IS NULL, Postgres does not. Commented Jul 7, 2020 at 0:01

2 Answers 2

4

Something like this:

ALTER TABLE listings ALTER COLUMN latitude TYPE FLOAT
    USING (CASE WHEN latitude = '' THEN 0.0 ELSE latitude::float END);

If you still have problems with bad characters, you can try:

ALTER TABLE listings ALTER COLUMN latitude TYPE FLOAT
    USING (CASE WHEN latitude = '' THEN 0.0 ELSE REGEXP_REPLACE(latitude, '[^-0-9.]', '', 'g') END);

This removes all non-digit like characters so the conversion should go ahead. Note that the value could still be a non-float (e.g. '1.2.3'), but that seems unlikely for the column.

Also, you might want to consider a numeric instead of float; fixed point arithmetic makes sense for geographic coordinates.

Sign up to request clarification or add additional context in comments.

2 Comments

Correct answer, but double precision is the data type for geographical coordinates.
Worked beautifully. Thanks a lot Gordon! The correct data type for geographic coordinates is probably a discussion for a new question!
0

I have no idea whether this is the best plan, but this is what I would do:

select count(*) from 
  (select (case 
             when nullif(latitude, '') is null then null
             else latitude
           end)::float
     from listings
  ) x;

If that blows up, then I would look at why it is blowing up and then add the new exclusion candidate to the case.

Once I find all the pathological cases, I would:

update listings 
   set latitude = case
                    when latitude = `` then null
                    when latitude = 'some other messed-up representation' then null
                    else latitude
                  end
;

1 Comment

Thanks, this would have got me there as well I think and is probably more complete if there were more other representations in the data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.