Pacerier Pacerier - 1 month ago 4
SQL Question

How to get second argument of Round() to work with columns?

I've a simple table with an integer column:

# setup table:
create table t(x int); insert t select 1;


The query
select round(1.234, 1) from t
returns
1.2
as expected.

However,
select round(1.234, x) from t
returns
1.2000
. (It should return
1.2
, as per the docs.)

Is this a bug? (Tested on version
5.5.10
and latest
5.6.24
.)

Or, is there any particular technical reason why columns cannot be used in the second argument of
round
?

How can we get
round
to work even while there are columns used in the second argument?

Answer

Consider using FORMAT instead of ROUND:

mysql> SELECT FORMAT(1.234, x) FROM ( SELECT 1 AS x ) y;
+------------------+
| FORMAT(1.234, x) |
+------------------+
| 1.2              |
+------------------+

If that is not satisfactory, file a bug about ROUND.

Comments