MySQL MySQL从varchar到float的数据类型转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8005445/
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 21:33:24  来源:igfitidea点击:

MySQL datatype conversion from varchar to float

mysql

提问by Shahid Karimi

How might I convert a varcharinto a floatin MySQL while executing a query?

在执行查询时如何将 avarchar转换为floatMySQL 中的 a?

回答by b10wf15h

You cannot cast the value in mysql using float type.

您不能使用 float 类型在 mysql 中转换值。

The type can use following values:

该类型可以使用以下值:

  • BINARY[(N)]
  • CHAR[(N)]
  • DATE
  • DATETIME
  • DECIMAL[(M[,D])]
  • SIGNED [INTEGER]
  • TIME
  • UNSIGNED [INTEGER]
  • 二进制[(N)]
  • 字符[(N)]
  • 日期
  • 约会时间
  • 十进制[(M[,D])]
  • 签名 [整数]
  • 时间
  • 无符号 [整数]

So in your case you have to use decimal, e.g:

因此,在您的情况下,您必须使用十进制,例如:

select cast(amount AS DECIMAL(10,2)) as 'float-value' from amounts

回答by mahimatrix

You can use this simple trick 0 + column_nameto convert it to float.

您可以使用这个简单的技巧0 + column_name将其转换为浮点数。

select 0 + column_name from table;