Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL datatype conversion from varchar to float

Tags:

mysql

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

like image 720
Shahid Karimi Avatar asked Nov 04 '11 06:11

Shahid Karimi


2 Answers

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
like image 138
b10wf15h Avatar answered Nov 05 '22 06:11

b10wf15h


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

select 0 + column_name from table;
like image 33
mahimatrix Avatar answered Nov 05 '22 07:11

mahimatrix