Rounding the ROUND Function

Moderator: NorbertKrupa

Post Reply
User avatar
JimKnicely
Site Admin
Site Admin
Posts: 1825
Joined: Sat Jan 21, 2012 4:58 am
Contact:

Rounding the ROUND Function

Post by JimKnicely » Wed Mar 13, 2013 12:37 pm

The SQL ROUND (X, Y) built-in function rounds the argument X to Y decimal places.

In most RDMBS systems the ROUND function will return the same result regardless if Y is an integer or a float.

MySQL:

Code: Select all

MySQL> SELECT ROUND(123.45678, 3);
+---------------------+
| ROUND(123.45678, 3) |
+---------------------+
|             123.457 |
+---------------------+
1 row in set (0.00 sec)

MySQL> SELECT ROUND(123.45678, 3.0);
+-----------------------+
| ROUND(123.45678, 3.0) |
+-----------------------+
|               123.457 |
+-----------------------+
1 row in set (0.00 sec)
SQL Server:

Image

Oracle:

Code: Select all

SQL> SELECT ROUND(123.45678, 3) FROM dual;

ROUND(123.45678,3)
------------------
           123.457

SQL> SELECT ROUND(123.45678, 3.0) FROM dual;

ROUND(123.45678,3.0)
--------------------
             123.457
Vertica:

Being the new kid on the block, Vertica had to mix things up a bit. As expected, the Vertica ROUND function rounds a value to a specified number of decimal places; however, it will retain the original scale and precision if Y is an integer and truncate the precision if Y is a float.

Code: Select all

dbadmin=> SELECT ROUND(123.45678, 3);
   ROUND
-----------
123.45700
(1 row)

dbadmin=> SELECT ROUND(123.45678, 3.0);
  ROUND
---------
123.457
(1 row)
The distinction is really noticeable when there are many decimal places in X:

Code: Select all

dbadmin=> SELECT ROUND(123.4567890123456789012345678901234567890, 5);
                   ROUND
-------------------------------------------
123.4567900000000000000000000000000000000
(1 row)

dbadmin=> SELECT ROUND(123.4567890123456789012345678901234567890, 5.0);
   ROUND
-----------
123.45679
(1 row)
Keep this in mind when writing SQL which includes the ROUND function for reports. You probably don’t want to display the value “123.4567900000000000000000000000000000000” is a report for the accounting folks :D

Have fun!
Jim Knicely

Image

Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.

Post Reply

Return to “Vertica Tips, Lessons and Examples”