Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truncate results from SQL to integer value

I have different data values (types), like 10.00 or 2.00. How can I get only its integer value, so I want to get 10 instead of 10.00 and 2 instead of 2.00?

like image 979
Darjeeling Avatar asked May 02 '26 17:05

Darjeeling


2 Answers

You can use the FLOOR or TRUNCATE function depending on your intention:

SELECT FLOOR(1.999)        --  1
SELECT FLOOR(-1.999)       -- -2
SELECT TRUNCATE(1.999, 0)  --  1
SELECT TRUNCATE(-1.999, 0) -- -1
like image 188
Salman A Avatar answered May 04 '26 12:05

Salman A


For example, you cound try to do this:

WHERE CONVERT(your_column, SIGNED INTEGER) AS num
FROM 
  table
ORDER BY 
  num;

Based on MySQL Documentation CONVERT() and CAST():

CONVERT() provides a way to convert data between different character sets. The syntax is:

CONVERT(expr USING transcoding_name)

In MySQL, transcoding names are the same as the corresponding character set names.

Besides for you also could work ROUND(X), ROUND(X,D):

Rounds the argument X to D decimal places. The rounding algorithm depends on the data type of X. D defaults to 0 if not specified. D can be negative to cause D digits left of the decimal point of the value X to become zero.

mysql> SELECT ROUND(150.000,2), ROUND(150,2);
+------------------+--------------+
| ROUND(150.000,2) | ROUND(150,2) |
+------------------+--------------+
|           150.00 |          150 |
+------------------+--------------+
like image 38
Ilia Avatar answered May 04 '26 12:05

Ilia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!