Nested CAST not working

≡放荡痞女 提交于 2019-12-07 03:04:15

问题


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


回答1:


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



回答2:


This query is working on the concept of nested cast.

cast(sum(cast(Column_name int )+ cast(Column_name as int)) as bigint) as payment from table_name



来源:https://stackoverflow.com/questions/10347624/nested-cast-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!