Chapter 03-Using Single-Row Functions to Customize Output(02)
Number Functions
ROUND:Rounds value to a specified deciaml
TRUNC:Truncates value to a specified decimal
MOD:Returns remainder fo division
Function | Result |
ROUND(45.926,2) | 45.93 |
TRUNC(45.926,2) | 45.92 |
MOD(1600,300) | 100 |
Demo-01:Using the ROUND Function
SQL> SELECT ROUND(45.923,2),ROUND(45.923,0),ROUND(45.923,-1) FROM dual; ROUND(45.923,2) ROUND(45.923,0) ROUND(45.923,-1) --------------- --------------- ---------------- 45.92 46 50 SQL> SELECT ROUND(45.923,2),ROUND(45.923,0),ROUND(45.923,-2) FROM dual; ROUND(45.923,2) ROUND(45.923,0) ROUND(45.923,-2) --------------- --------------- ---------------- 45.92 46 0 SQL> SELECT ROUND(45.923,2),ROUND(45.923,0),ROUND(75.923,-2) FROM dual; ROUND(45.923,2) ROUND(45.923,0) ROUND(75.923,-2) --------------- --------------- ---------------- 45.92 46 100
DUAL is a dummy table that you can use to view results from functions and calculations.
Demo-02:Using the TRUNC Functioin
SQL> SELECT TRUNC(45.923,2),TRUNC(45.923),TRUNC(45.923,-1) FROM DUAL; TRUNC(45.923,2) TRUNC(45.923) TRUNC(45.923,-1) --------------- ------------- ---------------- 45.92 45 40
Demo-03:Using the MOD Function
For all employees with the job title of Sales Representative,calulate the remainder of the salary after it is divided by 5000.
View Code
SQL> select employee_id,job_id ,MOD(salary,5000) from employees where job_id = 'SA_REP'; EMPLOYEE_ID JOB_ID MOD(SALARY,5000) ----------- ---------- ---------------- 150 SA_REP 0 151 SA_REP 4500 152 SA_REP 4000 153 SA_REP 3000 154 SA_REP 2500 155 SA_REP 2000 156 SA_REP 0 157 SA_REP 4500 158 SA_REP 4000 159 SA_REP 3000 160 SA_REP 2500 EMPLOYEE_ID JOB_ID MOD(SALARY,5000) ----------- ---------- ---------------- 161 SA_REP 2000 162 SA_REP 500 163 SA_REP 4500 164 SA_REP 2200 165 SA_REP 1800 166 SA_REP 1400 167 SA_REP 1200 168 SA_REP 1500 169 SA_REP 0 170 SA_REP 4600 171 SA_REP 2400 EMPLOYEE_ID JOB_ID MOD(SALARY,5000) ----------- ---------- ---------------- 172 SA_REP 2300 173 SA_REP 1100 174 SA_REP 1000 175 SA_REP 3800 176 SA_REP 3600 177 SA_REP 3400 178 SA_REP 2000 179 SA_REP 1200 30 rows selected.