MySQL 如何将字符串转换为十进制?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13245346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:23:23  来源:igfitidea点击:

How to convert string into a decimal?

mysqlruby-on-railsruby

提问by John Woo

I have two strings "0.31" and "0.0076" and they need to be stored in a decimal(10,2) column in MySQL. How do I do this conversion in ruby but not in mysql directly

我有两个字符串“0.31”和“0.0076”,它们需要存储在 MySQL 的小数(10,2)列中。我如何在 ruby​​ 中而不是在 mysql 中直接进行这种转换

回答by John Woo

try using CAST

尝试使用 CAST

SELECT CAST(colName AS DECIMAL(10,2))
FROM tableName

回答by Mudassir Hasan

use MySql conversion functions CAST or CONVERT . Read Here

使用 MySql 转换函数 CAST 或 CONVERT 。在这里阅读

 Select CAST(columnName as DECIMAL(10,2))

                or

 Select CONVERT(columnName,DECIMAL(10,2))

回答by Bajrang

OR you can do the job like

或者你可以做这样的工作

select format(columnname, 0) as formated from tablename  where condition