mysql - mysql 中如何将字符串转换为浮点型?

标签 mysql type-conversion

我有一个表,其中包含存储为字符串 (VARCHAR) 的纬度和经度值,我希望将其转换为 FLOAT (10,6)

但是,使用 CAST()CONVERT() 似乎没有一种简单的方法来执行此操作。

如何轻松转换这些列?这是一次性转换。

最佳答案

事实证明我只是在 CAST() 描述中缺少 DECIMAL:

DECIMAL[(M[,D])]

Converts a value to DECIMAL data type. The optional arguments M and D specify the precision (M specifies the total number of digits) and the scale (D specifies the number of digits after the decimal point) of the decimal value. The default precision is two digits after the decimal point.

因此,以下查询有效:

UPDATE table SET
latitude = CAST(old_latitude AS DECIMAL(10,6)),
longitude = CAST(old_longitude AS DECIMAL(10,6));

关于mysql - mysql 中如何将字符串转换为浮点型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57008126/

相关文章:

rust - 如何将 &mut T 类型转换为 &mut U 类型?

mysql - SQL将一列的值设置为同一张表中另一列的值

mysql - 使用 beginTransaction();和提交();在 magento 的管理 Controller 中

javascript - Laravel和Flutter通知系统

c++ - static_cast<int>(var) 和 *(int*)&var 有什么区别?

C# Windows 窗体类型转换错误

javascript - JSLint 错误 "Bad type."是什么意思?

mysql - Laravel - 如何插入带有保留字和破折号的行

php - 使用 PHP 和 MySQL 检查页面命中情况?

c++ - 将 struct 转换为 unsigned char *