mysql - 如何在mysql中将字符串转换为 float ?

标签 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中将字符串转换为 float ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7368163/

相关文章:

mysql - mysql中插入其他列值时如何结转某一列值?

mysql - 如何搭建SQL测试环境?

c - 是否有 C 函数将以 X 为基数的数字字符串转换为以 Y 为基数的字符串?

java - java中的switch语句接受哪些数据类型

mysql - 使用 MAX() 和 GROUP BY 未返回正确的结果

mysql - SQL给我里面的用户小于30的类(class)

c++ - 将字符串转换为 IP 地址时输出错误

隐式类型转换的 TypeScript 错误

c - 使用参数 C 运行程序

mysql - 哪个 RDMS 用于 Tableau 连接?