I have a procedure I am doing that displays odds but the client wants only significant digits to be shown. So, 1.50 would show as \'1.5\' and 1.00 would show as \'1\'.
Easiest way by far, just add zero!
Examples:
SET
@yournumber1="1.500",
@yournumber2="23.030",
@yournumber3="2.000",
@yournumber4="4.450"
;
SELECT
(@yournumber1+0),
(@yournumber2+0),
(@yournumber3+0),
(@yournumber4+0)
;
+------------------+------------------+------------------+------------------+
| (@yournumber1+0) | (@yournumber2+0) | (@yournumber3+0) | (@yournumber4+0) |
+------------------+------------------+------------------+------------------+
| 1.5 | 23.03 | 2 | 4.45 |
+------------------+------------------+------------------+------------------+
1 row in set (0.00 sec)
If the column your value comes from is DECIMAL
or NUMERIC
type, then cast it to string first to make sure the conversion takes place...ex:
SELECT (CAST(`column_name` AS CHAR)+0) FROM `table_name`;
For a shorter way, just use any built-in string function to do the cast:
SELECT TRIM(`column_name`)+0 FROM `table_name`;
Here's what worked for me:
SINGLE COLUMN:
SELECT TRIM(column_name)+0 AS column_name FROM table_name;
MULTIPLE COLUMNS:
SELECT
TRIM(column1)+0 AS column1,
TRIM(column2)+0 AS column2,
TRIM(column3)+0 AS column3,
FROM table_name;
Using ROUND or CEILING, in the query you just have to type:
SELECT ROUND(2/50)
or
SELECT CEILING(2/50)
Try this simply updated Query to remove decimal zeros
SELECT TRIM(TRAILING '.00' FROM price_column)
FROM table_name
Taking fragments of the others answers in this page I came to this conclusion:
SELECT ( IF(
myfield LIKE '%.%',
TRIM(TRAILING '.' FROM TRIM(TRAILING '0' FROM myfield)),
myfield
) ) FROM mytable
Cheers
EDIT: I would use the answer below by Christopher McGowan instead - adding 0 to the value, which is better, instead.
It's important to check there is actually a decimal point if doing trimming.
So I think you'd want to use:
SELECT TRIM(TRAILING '.' FROM TRIM(TRAILING '0' from yourfield)) AS yourfield
FROM yourtable
WHERE yourfield LIKE '%.%'