Categories
mysql

CAST DECIMAL to INT

I’m trying to do this:

SELECT CAST(columnName AS INT), moreColumns, etc
FROM myTable
WHERE ...

I’ve looked at the help FAQs here: http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html , it says I can do it like CAST(val AS TYPE), but it’s not working.

Trying to convert a decimal to int, real value is 223.00 and I want 223

You could try the FLOOR function like this:

SELECT FLOOR(columnName), moreColumns, etc 
FROM myTable
WHERE ...

You could also try the FORMAT function, provided you know the decimal places can be omitted:

SELECT FORMAT(columnName,0), moreColumns, etc 
FROM myTable
WHERE ...

You could combine the two functions

SELECT FORMAT(FLOOR(columnName),0), moreColumns, etc 
FROM myTable
WHERE ...