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?
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) .
@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.
Since latitude and longitude can take any value in the available range, it seems pretty clear that they are continuous.
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. "','".
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With