Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server latitude and longitude data type

I want to ask about what SQL Server data type to use for storing latitude and longitude.

Here's an example:

Latitude: -7.39755000 Longitude: 107.80627000 

If data type decimal, it won't save because SQL Server said:

cannot convert numeric data to numeric

as I remembered.

But if type data was float, SQL Server will convert 0 so the latitude will be -7.39755000. So I figure it out replace the last 0 with 1 - 9. is it the location will be not accurate?

Or maybe stick with decimal, but how to avoid the warning?

like image 600
Bobby Z Avatar asked Jun 24 '15 04:06

Bobby Z


People also ask

What data type is longitude and latitude in SQL?

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) .

What data type is latitude in SQL Server?

@g2server Latitude has the range [-90;90] so it could be stored in DECIMAL(8,6) . Longitude has the range [-180;180] so it should be stored in DECIMAL(9,6) . For consistency I think DECIMAL(9,6) for both should be preferred.

What type of data is longitude?

Since latitude and longitude can take any value in the available range, it seems pretty clear that they are continuous.

How do you insert Latitude and Longitude into a database?

php $lat=$_GET['lat']; $lng=$_GET['lng']; // write your insert query here to save $lat and $lng //get your mysql db connection ready $sql="insert into table_name (latitude, longitude) values('". $lat. "','".


1 Answers

DECIMAL(9,6) is what I normally use for both. If you need more precision, then try DECIMAL(12,9).

You could also use the GEOGRAPHY data type, but it will take more space, and I would only recommend this if you need SQL Servers spatial features (indexing etc).

For the actual conversion, try this:

CAST(@YourLat AS DECIMAL(12,9)) 
like image 184
g2server Avatar answered Sep 28 '22 01:09

g2server