| NUMBER FUNCTIONS IN ORACLE | | | | TRUNC(34.4556,3) |
| - Particularly in business everything is guided by | | | | Â Â Â Â Â Â Â Â Â 34.455 |
| numbers | | | | - Functions acting on Group of values. |
| - •         Fortunately Oracle, provides | | | | These Functions act, on values, of a column, in all rows, |
| functions which deal with numbers. | | | | of a table. |
| - •         Oracle functions deal with three | | | | The  most commonly used functions are |
| classes of numbers: | | | | Â |
|            Single values, group values AND | | | | Function       |
| list values. | | | | Â Â Â Â Â Â Â Â Â Â Â Description |
| ü       Single value, is one number, such as | | | | ---------------------- |
| 24.45. | | | | AVG(ColumnName)Â Â Â |
| ü       Group values, is all numbers in a | | | |                   Average of all values |
| column of a table. | | | | in the column. |
| ü       List values, is a series of numbers in | | | | COUNT(ColumnName)        number - of rows |
| columns of a row. | | | | or of values in column. |
| Lets best understand by an example consider the | | | | SUM(ColumnName)Â Â Â Â Â Â Â Â Â Â sum total, of all |
| climate table below. | | | | values in the column. |
| CityName      MinTemp       | | | | MIN(ColumnName)    |
| MaxTemp      DatedOn | | | |                   Returns least of all |
| Mumbai        33.33              | | | | values in that column. |
| 40.5Â Â Â Â Â Â Â Â 12-mar-2010 | | | | MAX(ColumnName)Â Â Â |
| Chennai        32.33 | | | |                   Returns highest of all |
| Â Â Â Â Â Â Â Â Â Â Â Â Â Â 42.50Â Â Â Â Â 12-mar-2010 | | | | values in that column. |
| Calcutta       34.45               | | | | STDDEV(Columnname)               |
| 38.0Â Â Â Â Â Â Â 12-mar-2010 | | | | Usual statistical meaning. |
| Delhi            36.89 | | | | VARIANCE(ColumnName)            |
| Â Â Â Â Â Â Â Â Â Â Â Â Â Â 46.89Â Â Â Â Â 12-mar-2010 | | | | Usual statistical meaning, |
| Mumbai        33.45 | | | |  |
| Â Â Â Â Â Â Â Â Â Â Â Â Â Â 45.89Â Â Â Â Â 13-mar-2010 | | | | Lets' consider the emp table, from the scott/tiger |
| Single value, is one value in a column. | | | | Â schema of oracle database |
| Group values, is values in a column, of all rows.for | | | | EMPNOÂ Â ENAMEÂ Â Â JOBÂ Â Â Â Â Â Â Â Â |
| example all values of MinTemp column. | | | | Â Â Â Â Â Â MGRÂ Â HIREDATEÂ Â Â Â |
| List Values, is collection of values in columns of a | | | | SALÂ Â Â Â Â Â Â COMMÂ Â DEPTNO |
| particular row. For example 33.33 and 40.5 are from | | | | 7369Â Â Â Â Â Â SMITHÂ Â Â Â Â Â Â |
| the first row of the above table. | | | | CLERKÂ Â Â Â Â Â Â 7902Â 17-DEC-80Â Â Â Â |
| - Functions acting on single values. | | | | 800Â Â Â Â Â Â Â Â Â 20Â Â Â Â Â Â Â Â Â Â Â Â 20 |
| Function       | | | | 7499      ALLEN       |
| Â Â Description | | | | SALESMANÂ Â 7698Â 20-FEB-81Â Â Â Â 1600Â |
| ---------------------- | | | | Â Â Â Â Â Â 300Â Â Â Â Â Â Â Â Â Â 30 |
| Value1 + value2Â Â Â Â Â Â Â | | | | 7521Â Â Â Â Â Â WARDÂ Â Â Â Â Â Â SALESMANÂ Â |
| Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Here + function, adds | | | | 7698Â 22-FEB-81Â Â Â Â 1250Â Â Â Â Â Â Â Â |
| value1 with value2. | | | | 500Â Â Â Â Â Â Â Â Â 30 |
| Value1 – value2       | | | | 7566      JONES       |
|                Here – function, | | | | MANAGER   7839  02-APR-81     2975 |
| Substracts value2 from value1. | | | | Â Â Â Â Â Â Â 20Â Â Â Â Â Â Â Â Â Â Â 20 |
| Value1 * value2Â Â Â Â Â Â Â | | | | 7654Â Â Â Â Â Â MARTINÂ Â Â Â Â SALESMANÂ Â |
| Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Here * function, multiplies | | | | 7698Â 28-SEP-81Â Â Â Â Â 1250 Â Â Â Â Â Â Â |
| value2 with value1. | | | | 1400Â Â Â Â Â Â Â Â 30 |
| Value1 / value2Â Â Â Â Â Â Â | | | | 7698Â Â Â Â Â Â BLAKEÂ Â Â Â Â Â Â |
| Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Here / function, Divides | | | | MANAGERÂ Â Â 7839Â Â 01-MAY-81Â Â Â Â Â |
| value1 with value2. | | | | 2850Â Â Â Â Â Â Â Â 30Â Â Â Â Â Â Â Â Â Â 30 |
| ABS(value)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Returns | | | | 7782Â Â Â Â Â Â CLARKÂ Â Â Â Â Â Â |
| ABSolute value, ie returns positive value. | | | | MANAGER   7839  09-JUN-81      |
| SIGN(value)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â | | | | 2450Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 10Â Â Â Â Â Â Â |
| Returns, | | | | Â Â Â Â 10 |
| Â Â 1Â if value positive, | | | | 7788Â Â Â Â Â Â SCOTTÂ Â Â Â Â Â ANALYSTÂ Â Â |
| Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â -1 if value negative, | | | | 7566Â 19-APR-87Â Â Â Â Â Â 3000Â Â Â Â Â Â Â Â Â |
| Â Â Â Â Â 0 if value zero. | | | | Â Â Â Â Â Â Â 20Â Â Â Â Â Â Â Â Â Â Â 20 |
| CEIL(value)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Returns | | | | 7839Â Â Â Â Â Â KINGÂ Â Â Â Â Â Â Â Â |
| Integer, just above the given value or equal to the | | | | PRESIDENTÂ Â Â Â Â Â Â Â Â Â 17-NOV-81Â Â Â |
| value. | | | | Â Â Â 5000Â Â Â Â Â Â Â 10Â Â Â Â Â Â Â Â Â Â Â 10 |
| FLOOR(value) Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Returns | | | | 7844Â Â Â Â Â Â TURNERÂ Â Â Â Â SALESMANÂ Â |
| Integer, just below the given or equal to the value. | | | | 7698Â 08-SEP-81Â Â Â Â Â Â 1500Â Â Â Â Â Â Â Â Â |
| MOD(value,divisor)Â Â Â Â Â Â Returns, reminder of | | | | 0Â Â Â Â Â Â 30Â Â Â Â Â Â Â Â Â Â Â Â 20 |
| value/divisor. | | | | 7876Â Â Â Â Â Â ADAMSÂ Â Â Â Â Â |
| NVL(value,substitute)Â Â Â Â Â Â Â Â Â Â Â Â Substitutes | | | | CLERKÂ Â Â Â Â Â Â 7788Â 23-MAY-87Â Â Â Â Â |
| value, if value is null. | | | | 1100 Â Â Â Â Â Â Â Â 20Â Â Â Â Â Â Â Â Â 20 |
| POWER(value,exponent)Â Â Returns, Value raised to | | | | 7900Â Â Â Â Â Â JAMESÂ Â Â Â Â Â Â |
| an exponent. | | | | CLERKÂ Â Â Â Â Â Â 7698Â 03-DEC-81Â Â Â Â Â Â |
| SQRT(value)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â | | | | 950Â Â Â Â Â Â Â Â Â Â 30Â Â Â Â Â Â Â Â Â 30 |
| Returns, Square root of value | | | | 7902Â Â Â Â Â Â FORDÂ Â Â Â Â Â Â Â |
| TRUNC(value, precision)Â Â Returns, Value truncated | | | | ANALYSTÂ Â Â 7566Â 03-DEC-81Â Â Â Â Â Â 3000 |
| to precision. | | | | Â Â Â Â Â Â Â Â 20Â Â Â Â Â Â Â Â Â 20 |
| ROUND(value,precision)Â Â Â Returns, value Rounded | | | | 7934Â Â Â Â Â Â MILLERÂ Â Â Â Â Â |
| to precision. | | | | CLERKÂ Â Â Â Â Â Â 7782Â 23-JAN-82Â Â Â Â Â Â |
| Â | | | | 1300Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 10Â Â Â Â Â |
| Before we see some examples, lets' understand what | | | | Â Â Â Â 10 |
| does dual mean. Dual is a dummy table in oracle | | | | Â |
| database, which is used for testing sql statements. | | | | Lets work some examples, the letters in blue are the |
| Let us work some examples, making use of functions | | | | results returned by the oracle database,in reply to a |
| acting on single values. Here letters in blue are results | | | | query |
| given by oracle when respective query is executed | | | | Find the maximum salary paid |
| SQL>select 6 + 2 from dual; | | | | SQL> select MAX(sal) from emp; |
| 6 + 2 | | | | MAX(SAL) |
| Â Â 8 | | | | Â Â Â 5000 |
| SQL>select 6 - 2 from dual; | | | | Find the minimum salary paid |
| 6 - 2 | | | | SQL> select MIN(sal) from emp; |
| Â Â 4 | | | | Â MIN(SAL) |
| SQL>select 6 * 2 from dual; | | | | Â Â Â Â Â Â 800 |
| 6 * 2 | | | | Find the average salary paid |
| Â Â 12 | | | | SQL> select AVG(sal) from emp; |
| SQL>select 6 / 2 from dual; | | | | Â AVG(SAL) |
| 6/2 | | | | 2073.21429 |
| Â 3 | | | | Find the total amount paid as salaries to the |
| SQL>select ABS(-121) from dual; | | | | employees |
| ABS(-121) | | | | SQL> select SUM(sal) from emp; |
| Â Â Â Â Â Â 121 | | | | Â SUM(SAL) |
| SQL>select ABS(121) from dual; | | | | Â Â Â Â 29025 |
| ABS(121) | | | | COUNT functions usage is situational: |
| Â Â Â Â 121 | | | | - Is used to count the number of rows in a table, if the |
| SQL> select ABS(-121) from dual; | | | | argument is *(asterisk) |
| Â ABS(-121)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â | | | | Â SQL> select COUNT(*) from emp; |
| Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â | | | | Â COUNT(*) |
| Â Â Â Â Â Â Â Â Â Â Â Â Â Â | | | | Â Â Â Â Â Â Â 14 |
| Â Â Â Â Â Â | | | | - Is used to count number of available data in a column |
| Â Â Â Â Â Â Â Â Â | | | | of a table. |
| SQL> select sign(121) from dual; | | | | Find the number of, Â NOT NULL values, in the column |
| Â Â Â Â Â Â Â Â Â Â Â Â Â | | | | mgr of emp table? |
| Â Â Â Â Â Â Â Â Â Â Â Â Â Â | | | | SQL> select COUNT(mgr) from emp; |
| Â Â Â Â Â Â Â Â | | | | COUNT(MGR) |
| Â Â Â Â Â Â Â | | | | Â Â Â Â Â Â Â 13 |
| SQL> select sign(-121) from dual; | | | | - DISTINCT key word, can be used, with every |
| Â Â Â Â Â Â Â Â Â Â Â Â Â | | | | GROUP Function, but its usage is more felt, with |
| Â Â Â Â Â Â Â Â Â Â Â Â Â Â | | | | COUNT function. Let us see apply DISTINCT to find, |
| Â Â Â Â Â Â Â | | | | how many types of jobs are there in emp table |
| Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â | | | | SQL> select COUNT(DISTINCT JOB) from emp; |
| Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â | | | | COUNT(DISTINCTJOB) |
| SQL> select sign(0) from dual; | | | | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 5 |
| Â Â | | | | There are 5 types of JOB are in emp table. |
| Â Â Â Â Â Â Â Â Â Â Â | | | | Â |
| Â Â Â Â Â Â Â Â Â Â Â Â Â Â | | | | Lets write a query to find number, of NULL values in |
| Â Â Â Â Â Â Â Â 0Â Â Â Â Â Â Â Â Â Â Â Â Â | | | | mgr column of emp table |
| SQL> select CEIL(121.22) from dual; | | | | SQL> select COUNT(*)-COUNT(mgr) from emp; |
| CEIL(121.22) | | | | COUNT(*)-COUNT(MGR) |
| Â Â Â Â Â Â Â Â 122 | | | | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 1 |
| SQL> select CEIL(-121.22) from dual; | | | | There is one NULL, in mgr column, of emp table. |
| CEIL(-121.22) | | | | Remember |
|         -121 | | | | ü      Asterisk can not be used with any |
| SQL> select CEIL(121) from dual; | | | | other GROUP function, other than COUNT. |
|  CEIL(121) | | | | ü      DISTINCT finds its usage mostly with |
| Â Â Â Â Â Â 121 | | | | COUNT function. |
| SQL> select CEIL(-121) from dual; | | | | ü      COUNT can be used either with |
| CEIL(-121) | | | | character column or number column, where as other |
| Â Â Â Â Â -121 | | | | GROUP functions can be used only with numerical |
| SQL> select FLOOR(121.22) from dual; | | | | data. |
| FLOOR(121.22) | | | | ü      By now you should have observed, |
| Â Â Â Â Â Â Â Â Â 121 | | | | COUNT does not perform any arithmetic operations |
| Â | | | | with the values of a column but it either counts number |
| SQL> select FLOOR(-121.22) from dual; | | | | of values in a column or number rows in a table. |
| FLOOR(-121.22) | | | | ü      The alternative to DISTINCT is ALL, |
| Â Â Â Â Â Â Â Â Â -122 | | | | which is the default. |
| SQL> select FLOOR(121) from dual; | | | | - Functions acting on list of values |
| FLOOR(121) | | | | These functions act on values, in two or more |
| Â Â 121 | | | | columns, of a row, in a table |
| SQL> select FLOOR(-121) from dual; | | | | The most frequently used functions are described in |
| FLOOR(-121) | | | | table below |
| Â Â Â Â Â Â -121 | | | | Â |
| SQL> select MOD(10,3) from dual; | | | | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â |
| Â MOD(10,3) | | | | Â Â Â Â Â Â Â Â Â Â Â Â DESCRIPTION |
| Â Â Â Â Â Â Â Â 1 | | | | LEAST(value1,value2,value3,..)Â Â Â Â Â |
| SQL> select MOD(10,2) from dual; | | | | Â Â Â Â Â Â Â Â Â Â Returns least value,among the |
| Â MOD(10,2) | | | | values in the argument list. |
| Â Â Â Â Â Â Â Â 0 | | | | GREATEST(value1,value2,value3,..)Â Â Â Returns |
| SQL> select MOD(10,0) from dual; | | | | greatest value, among the values in the |
|  MOD(10,0) | | | | argument                |
| Â Â Â Â Â Â Â 10 | | | | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â |
| SQL> select MOD(0,10) from dual; | | | | Â Â Â Â Â Â Â Â Â Â Â Â List |
|  MOD(0,10) | | | | Consider the StudentMarks table below, with |
| Â Â Â Â Â Â Â Â 0 | | | | columns as student name, marks in physics, maths, |
| SQL> select MOD(0,0) from dual; | | | | Chemistry |
|  MOD(0,0) | | | | StudentName            MathMarks  |
|         0 | | | | PhysicsMarks            ChemistryMarks |
| Consider the climate table below | | | | --------------------------- |
|  | | | | Lova Raju                     |
| CityName         MinTemp     | | | | 67                |
| MaxTemp            DatedOn | | | | 76                         89 |
| Secunderabad          27     | | | | Siri                        |
| Â Â Â Â Â Â Â Â Â 33Â Â Â Â Â Â Â Â Â Â Â Â 12-feb-2008 | | | | Â Â Â Â Â 100Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â |
| Hyderabad             24     | | | | 99                         99 |
|          35            | | | | Viji                        |
| 23-nov-2008 | | | | Â Â Â Â Â Â 99Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â |
| Ahmedabad            34     | | | | 90                         88 |
|                             | | | | Koti                      |
| Â Â Â Â Â 26-mar-2008 | | | | Â Â Â Â Â Â 98Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â |
| Gaziabad               23     | | | | 88              |
| Â Â Â Â Â Â Â Â Â 34Â Â Â Â Â Â Â Â Â Â Â Â | | | | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 87 |
| 24-sep-2008 | | | | Â |
| Â | | | | Now write a query that would display student name |
| On observing the above table, we find that in 3rd row, | | | | and highest mark obtained, among the three subjects |
| value in, column MaxTemp is unavailable. Now by | | | | Sql>select |
| making use of NVL function we can substitute the | | | | stryMarks) as HighestMark from StudentMarks; |
| unavailable data with some value. | | | | ENAMEÂ Â Â Â Â HIGHESTMARK |
| Select CityName, NVL(MaxTemp,33.5) from dual; | | | | Lova Raju            89 |
| CityName              MaxTemp | | | | Siri                     100 |
|  Secunderabad         33 | | | | Viji                       99 |
|  Hyderabad               35 | | | | Koti                      98 |
|  Ahmedabad             33.5 | | | |  |
|  Gaziabad                 34 | | | | Write a query that would display student name and |
| Remember | | | | the least mark obtained, among the three subjects |
| ü      NVL function does not update the data | | | | Sql> select StudentName, |
| base. It temporarily substitutes and displays on the | | | | LEAST(MathMarks,PhysicsMarks,ChemistryMarks) as |
| screen. | | | | LowestMark from StudentMarks; |
| SQL> select POWER (2, 3) from dual; | | | | - NULLs in -List Value Functions and Single Value |
| POWER (2,3) | | | | Functions: |
| Â Â Â Â Â Â Â Â 8 | | | | - List value functions treat NULL values same as that |
| SQL> select SQRT(4) from dual; | | | | of single value functions. |
| Â Â SQRT(4) | | | | - Single value functions produce NULL result, if any |
| Â Â Â Â Â Â Â Â 2 | | | | value is NULL. |
| SQL> select ROUND(34.4556,2) from dual; | | | | - List value functions produce NULL result, if any value |
| ROUND(34.4556,2) | | | | in the list is NULL |
| Â Â Â Â Â Â Â Â Â Â 34.46 | | | | NULLS in - GROUP Value Functions : |
| SQL> select ROUND(34.4556,3) from dual; | | | | - Group value functions treat NULL values differently |
| ROUND(34.4556,3) | | | | than single value functions. |
| Â Â Â Â Â Â Â Â Â 34.456 | | | | - Group value functions ignore NULL values and go |
| SQL> select ROUND(-34.4556,2) from dual; | | | | ahead with the result. |
| ROUND(-34.4556,2) | | | | Â Â Â Â Â Â Â Â Â Â Â Remember |
|           -34.46 | | | | ü      Group value functions, ignore NULL |
| SQL> select TRUNC(34.4556,2) from dual; | | | | values |
| TRUNC(34.4556,2) | | | | ü      Single value and List value functions, |
|           34.45 | | | | don't ignore NULL values. |
| SQL> select TRUNC(34.4556,3) from dual; | | | | |