如何在mysql中将字符串转换为浮点数? [英] How can I convert a string to a float in mysql?

查看:1568
本文介绍了如何在mysql中将字符串转换为浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a table containing latitude and longitude values stored as strings (VARCHAR) which I'd like to convert to FLOAT (10,6).

但是,使用CAST()CONVERT()似乎并不是一种简单的方法.

However there doesn't appear to be a straightforward way to do this using CAST() or CONVERT().

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

How can I convert these columns easily? This is a one-time conversion.

推荐答案

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

It turns out I was just missing DECIMAL on the CAST() description:

DECIMAL[(M[,D])]

DECIMAL[(M[,D])]

将值转换为DECIMAL数据类型.可选参数M和D指定十进制值的精度(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中将字符串转换为浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆