Friday, 1 April 2016

SQL Round

This will rounds numbers to a given number of digits of precision.

     Syntax: round (value, precision)      

 Ex:
SQL> select round(123.2345), round(123.2345,2), round(123.2354,2) from dual;

        ROUND(123.2345)  ROUND(123.2345,0) ROUND(123.2345,2) ROUND(123.2354,2)
                   ---------------------   ------------------------  -----------------------  -----------------------
                                123                          123                            123.23                      123.24

SQL> select round(123.2345,-1), round(123.2345,-2), round(123.2345,-3),round(123.2345,-4) from dual;
  
ROUND(123.2345,-1) ROUND(123.2345,-2) ROUND(123.2345,-3) ROUND(123.2345,-4)
           ------------------------  -------------------------  ------------------------   ------------------------
                           120                                100                            0                                         0

SQL> select round(123,0), round(123,1), round(123,2) from dual;

ROUND(123,0) ROUND(123,1) ROUND(123,2)
-----------------  -----------------  ----------------
         123                   123                               123

SQL> select round(-123,0), round(-123,1), round(-123,2) from dual;

ROUND(-123,0) ROUND(-123,1) ROUND(-123,2)
------------------  -----------------   -------------------
         -123                 -123                    -123

SQL> select round(123,-1), round(123,-2), round(123,-3), round(-123,-1), round(-123,- 2), round(-123,-3) from dual;

ROUND(123,-1) ROUND(123,-2) ROUND(123,-3) ROUND(-123,-1) ROUND(-123,-2)        
ROUND(-123,-3)
      -------------- ------------- ------------- -------------- -------------- --------------
            120           100             0               -120               -100              0

SQL> select round(null,null), round(0,0), round(1,1), round(-1,-1), round(-2,-2) from dual;

ROUND(NULL,NULL) ROUND(0,0) ROUND(1,1) ROUND(-1,-1) ROUND(-2,-2)
-----------------------  --------------  --------------  ----------------  ----------------
                                             0                       1                   0                    0

No comments:

Post a Comment