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
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 ...
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).
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.
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.
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 ...
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
(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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With