What datatype to use when storing latitude and longitude data in SQL databases? [duplicate]
When storing latitude or longitude data in an ANSI SQL compliant database, what datatype would be most appropriate? Should float
be used, or decimal
, or ...?
I'm aware that Oracle, MySql, and SQL Server have added some special datatypes specifically for handling geo data, but I'm interested in how you would store the information in a "plain vanilla" SQL database.
Solution 1:
For latitudes use: Decimal(8,6)
, and longitudes use: Decimal(9,6)
If you're not used to precision and scale parameters, here's a format string visual:
Latitude and Longitude
##.######
and ###.######
To 6 decimal places should get you to around ~10cm of accuracy on a coordinate.
Solution 2:
We use float, but any flavor of numeric with 6 decimal places should also work.
Solution 3:
You should take a look at the new Spatial data-types that were introduced in SQL Server 2008. They are specifically designed this kind of task and make indexing and querying the data much easier and more efficient.
http://msdn.microsoft.com/en-us/library/bb933876(v=sql.105).aspx