rafis
rafis

Reputation: 243

PostgreSQL select query to extract latitude and longitude from a point

What SELECT query should be used to extract latitude and longitude from a point?
I cannot use PostGIS.

Example point (point type value) stored in the database:

   my_point
--------------
(50.850,4.383)

Expected result after executing the query:

  lat  |  lng
---------------
50.850 | 4.383

The query below works fine but it does not look efficient.

SELECT 
    split_part(trim(my_point::text, '()'), ',', 1)::float AS lat, 
    split_part(trim(my_point::text, '()'), ',', 2)::float AS lng
FROM my_table;

Upvotes: 9

Views: 11806

Answers (2)

ruhanbidart
ruhanbidart

Reputation: 4824

Another option would be:

SELECT 
    ST_X(point) as longitude, 
    ST_Y(point) as latitude
FROM your_table_name

Upvotes: 0

Richard Huxton
Richard Huxton

Reputation: 22893

Always Read The Fine Manuals

It is possible to access the two component numbers of a point as though the point were an array with indexes 0 and 1. For example, if t.p is a point column then SELECT p[0] FROM t retrieves the X coordinate and UPDATE t SET p1 = ... changes the Y coordinate. In the same way, a value of type box or lseg can be treated as an array of two point values.

Upvotes: 15

Related Questions