MySQL datatype conversion from varchar to float

前端 未结 2 666
我在风中等你
我在风中等你 2020-12-15 09:41

How might I convert a varchar into a float in MySQL while executing a query?

相关标签:
2条回答
  • 2020-12-15 10:19

    You cannot cast the value in mysql using float type.

    The type can use following values:

    • BINARY[(N)]
    • CHAR[(N)]
    • DATE
    • DATETIME
    • DECIMAL[(M[,D])]
    • SIGNED [INTEGER]
    • TIME
    • UNSIGNED [INTEGER]

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

    select cast(amount AS DECIMAL(10,2)) as 'float-value' from amounts
    
    0 讨论(0)
  • 2020-12-15 10:20

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

    select 0 + column_name from table;
    
    0 讨论(0)
提交回复
热议问题