Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CAST DECIMAL to INT

Tags:

mysql

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

like image 779
Drahcir Avatar asked Dec 16 '11 16:12

Drahcir


People also ask

How do I convert decimal to int in mysql?

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 ... SELECT FORMAT(FLOOR(columnName),0), moreColumns, etc FROM myTable WHERE ...

How do I cast as decimal in SQL?

Use the CAST() function to convert an integer to a DECIMAL data type. This function takes an expression or a column name as the argument, followed by the keyword AS and the new data type. In our example, we converted an integer (12) to a decimal value (12.00).

How do you convert decimal to int in Python?

In Python, you can simply use the bin() function to convert from a decimal value to its corresponding binary value. And similarly, the int() function to convert a binary to its decimal value. The int() function takes as second argument the base of the number to be converted, which is 2 in case of binary numbers.

Can Int32 have decimals?

You can call the Parse or TryParse method to convert the string representation of an Int32 value to an Int32. The string can contain either decimal or hexadecimal digits.


2 Answers

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 ...  
like image 114
RolandoMySQLDBA Avatar answered Sep 24 '22 22:09

RolandoMySQLDBA


A more optimized way in mysql for this purpose*:

SELECT columnName DIV 1 AS columnName, moreColumns, etc FROM myTable WHERE ... 

Using DIV 1 is a huge speed improvement over FLOOR, not to mention string based functions like FORMAT

Speed of MySQL integer division Bar Chart (graphic from Roland Bouman's blog)

mysql> SELECT BENCHMARK(10000000,1234567 DIV 7) ; +-----------------------------------+ | BENCHMARK(10000000,1234567 DIV 7) | +-----------------------------------+ |                                 0 | +-----------------------------------+ 1 row in set (0.83 sec)  mysql> SELECT BENCHMARK(10000000,1234567 / 7) ; +---------------------------------+ | BENCHMARK(10000000,1234567 / 7) | +---------------------------------+ |                               0 | +---------------------------------+ 1 row in set (7.26 sec)  mysql> SELECT BENCHMARK(10000000,FLOOR(1234567 / 7)) ; +----------------------------------------+ | BENCHMARK(10000000,FLOOR(1234567 / 7)) | +----------------------------------------+ |                                      0 | +----------------------------------------+ 1 row in set (8.80 sec) 

(*) NOTE: As pointed by Grbts, be aware of the behaviour of DIV 1 when used with non unsigned/positive values.

like image 42
David Strencsev Avatar answered Sep 23 '22 22:09

David Strencsev