SQL Server纬度和经度数据类型

时间:2022-05-06 16:33:09

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

我想问一下用于存储纬度和经度的SQL Server数据类型。

Here's an example:

这是一个例子:

Latitude: -7.39755000
Longitude: 107.80627000

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

如果数据类型为十进制,则不会保存,因为SQL Server说:

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?

但是如果类型数据是浮点数,SQL Server将转换为0,因此纬度将为-7.39755000。所以我想出来用1 - 9替换最后的0。是不是位置不准确?

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

或者可能坚持使用小数,但如何避免警告?

1 个解决方案

#1


23  

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 use it if you need SQL Servers spatial features (indexing etc).

DECIMAL(9,6)是我通常用于两者的东西。如果您需要更高的精度,请尝试DECIMAL(12,9)。您也可以使用GEOGRAPHY数据类型,但它将占用更多空间,我只会在您需要SQL Server空间功能(索引等)时使用它。

For the actual conversion, try this:

对于实际转换,请尝试以下方法:

CAST(@YourLat AS DECIMAL(12,9))

#1


23  

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 use it if you need SQL Servers spatial features (indexing etc).

DECIMAL(9,6)是我通常用于两者的东西。如果您需要更高的精度,请尝试DECIMAL(12,9)。您也可以使用GEOGRAPHY数据类型,但它将占用更多空间,我只会在您需要SQL Server空间功能(索引等)时使用它。

For the actual conversion, try this:

对于实际转换,请尝试以下方法:

CAST(@YourLat AS DECIMAL(12,9))