27

How can I convert a string to a double precision in PostgreSQL ?

I tried something like :

update points set latitude2 = cast(latitude as double) ;

where latitude is a string and latitude2 is a double. I just can't get it to work.

RolandoMySQLDBA
  • 182,700
  • 33
  • 317
  • 520
yazz.com
  • 509
  • 1
  • 6
  • 10
  • 6
    "can't get it to work" is not a valid Postgres error message. Please post the complete error (or describe what exactly isn't working) –  Jul 11 '11 at 07:01
  • Eww this is a really bad question for another reason because you're asking about Longitude and Latitude and they shouldn't be stored as a doubles anyway. – Evan Carroll Aug 02 '18 at 18:57

1 Answers1

55

double is not a postgres data type:

select cast('324342' as double);
ERROR:  type "double" does not exist
LINE 1: select cast('324342' as double);
                                ^

but double precision is:

select cast('132342' as double precision);
| float8 |
| :----- |
| 132342 |

So try:

update points set latitude2 = cast(latitude as double precision) ;

if you prefer, you can use float * instead, because according to the docs:

float with no precision specified is taken to mean double precision

select cast('132342' as float);
| float8 |
| :----- |
| 132342 |

db<>fiddle here


* or float(n) where 25<=n<=53 because that is also taken to mean double precision

Jack Douglas
  • 39,869
  • 15
  • 101
  • 176