Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get X Y values from Point Geometry or Geography

Tags:

sql-server

I'm looking for the MSSQL equivalent of the following mySQL statement:

SELECT Y(location) as Lat, X(location) as Lon from myTable; 

I want to get the X and Y values of my geography I have stored as a geography data type.

I know STAsText(Location) as Location will give me the WKT... i just need the X and Y number values.

like image 210
capdragon Avatar asked Jun 01 '12 19:06

capdragon


People also ask

What is geography data type in SQL?

The geography spatial data type, geography, is implemented as a . NET common language runtime (CLR) data type in SQL Server. This type represents data in a round-earth coordinate system. The SQL Server geography data type stores ellipsoidal (round-earth) data, such as GPS latitude and longitude coordinates.

What should be the datatype of latitude and longitude?

p. precision you should use DECIMAL . Latitudes range from -90 to +90 (degrees), so DECIMAL(10,8) is ok for that, but longitudes range from -180 to +180 (degrees) so you need DECIMAL(11,8) .

How can we store geolocation in SQL?

You can use STDistance like this. DECLARE @g geography; DECLARE @h geography; SET @g = geography::STGeomFromText('POINT(-122.35900 47.65129)', 4326); SET @h = geography::STGeomFromText('POINT(-122.34720 47.65100)', 4326); SELECT @g. STDistance(@h);


1 Answers

For GEOGRAPHY:

SELECT location.Lat as Lat, location.Long as Lon from myTable; 

For GEOMETRY:

SELECT location.STY as Lat, location.STX as Lon from myTable; 
like image 94
Mithrandir Avatar answered Sep 28 '22 05:09

Mithrandir