Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested CAST not working

Why is a nested cast NOT working in MySQL? (It does using SQL Server)

select cast(cast(myColumn as decimal(5,2)) as int) from myTable 

SQLFiddle Example

like image 867
juergen d Avatar asked Apr 27 '12 08:04

juergen d


1 Answers

According to the manual:

CAST(expr AS type) [...]

CONVERT(expr,type) [...]

The type can be one of the following values:

  • BINARY[(N)]

  • CHAR[(N)]

  • DATE

  • DATETIME

  • DECIMAL[(M[,D])]

  • SIGNED [INTEGER]

  • TIME

  • UNSIGNED [INTEGER]

So, just follow the manual:

SELECT CAST(CAST(myColumn AS DECIMAL(5,2)) AS SIGNED) FROM myTable

or

SELECT CAST(CAST(myColumn AS DECIMAL(5,2)) AS UNSIGNED) FROM myTable
like image 118
Andriy M Avatar answered Oct 05 '22 07:10

Andriy M