Built in Functions
Built in Functions
Chittaranjan Pradhan
Employee Table
Group Functions
Chittaranjan Pradhan
School of Computer Engineering,
KIIT University
4.1
Built-in Functions
Built-in Functions
Chittaranjan Pradhan
DUAL Table
1 DUAL Table Employee Table
Built-in Functions
Group Functions
2 Employee Table
Scalar Functions
Date Functions
Numeric Functions
3 Built-in Functions Character Functions
Conversion Functions
Misc. Functions
4 Group Functions
5 Scalar Functions
Date Functions
Numeric Functions
Character Functions
Conversion Functions
Misc. Functions
4.2
Built-in Functions
DUAL Table
Chittaranjan Pradhan
DUAL Table
Employee Table
Built-in Functions
DUAL Table
Group Functions
DUAL table is a small worktable, which consists of only one Scalar Functions
Date Functions
column DUMMY and a single row with value X of VARCHAR2 Numeric Functions
4.3
Built-in Functions
Employee Table
Chittaranjan Pradhan
DUAL Table
Employee Table
Built-in Functions
Group Functions
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-DEC-80 800 20 Scalar Functions
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 Date Functions
Numeric Functions
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 Character Functions
7566 JONES MANAGER 7839 02-APR-81 2975 20 Conversion Functions
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 Misc. Functions
4.4
Built-in Functions
Built-in Functions
Chittaranjan Pradhan
DUAL Table
Employee Table
Built-in Functions
Built-in Functions
Group Functions
The built-in functions provide a powerful tool for the Scalar Functions
enhancement of a basic query. They serve the purpose of Date Functions
Numeric Functions
manipulating data items and returning a result. Functions are Character Functions
Conversion Functions
of two types: Misc. Functions
4.5
Built-in Functions
Group Functions
Chittaranjan Pradhan
The group or aggregate functions perform an operation on a
group of rows and return one result. The different aggregate
DUAL Table
functions are:
Employee Table
Group Functions
This function counts the number of rows without considering Scalar Functions
NULL values Date Functions
Numeric Functions
Character Functions
SELECT COUNT(MGR) FROM EMP; Conversion Functions
Misc. Functions
SELECT COUNT(DISTINCT MGR) FROM EMP;
COUNT(MGR) COUNT(DISTINCT MGR)
13 6
COUNT(*)
It counts the number of rows including NULL values
Employee Table
It finds the sum of all values in a column ignoring the NULL
Built-in Functions
values
Group Functions
AVG([DISTINCT] column)
It finds the average of all values in a column ignoring the NULL
values
SELECT AVG(SAL) FROM EMP;
AVG(SAL)
2075.35
4.7
Built-in Functions
Group Functions...
Chittaranjan Pradhan
Employee Table
It finds the maximum value in the column ignoring the NULL
Built-in Functions
values
Group Functions
MIN([DISTINCT] column)
It finds the minimum value in the column ignoring the NULL
values
SELECT MIN(SAL) FROM EMP;
MIN(SAL)
800
4.8
Built-in Functions
Scalar Functions
Chittaranjan Pradhan
Employee Table
These functions act on one value at a time. There are various Built-in Functions
types of scalar functions: Group Functions
4.9
Built-in Functions
Date Functions
Chittaranjan Pradhan
The date values are stored internally with day, month, year,
hour, minute and second information. The different date DUAL Table
Built-in Functions
SYSDATE Group Functions
It is the pseudo column that returns the system’s current date Scalar Functions
Date Functions
Numeric Functions
ADD_MONTHS(date, n)
It adds calendar months to a date
SELECT ADD_MONTHS(HIREDATE, 4) FROM EMP WHERE
EMP_NO=7369;
ADD_MONTHS(HIREDATE,4)
17-APR-81
4.10
Built-in Functions
Date Functions...
Chittaranjan Pradhan
DUAL Table
LAST_DAY(date) Employee Table
Built-in Functions
It returns the last day of the month
Group Functions
Scalar Functions
SELECT LAST_DAY(SYSDATE) FROM DUAL; Date Functions
Numeric Functions
LAST_DAY(SYSDATE) Character Functions
Conversion Functions
31-JAN-13
Misc. Functions
MONTHS_BETWEEN(date1, date2)
It finds the number of months between two dates
SELECT MONTHS_BETWEEN(SYSDATE,’23-JAN-89’) FROM
DUAL;
MONTHS_BETWEEN(SYSDATE,’23-JAN-89’)
287.90
4.11
Built-in Functions
Date Functions...
Chittaranjan Pradhan
NEXT_DAY(date, ’day’)
DUAL Table
It finds the next occurrence of a day from the given date Employee Table
Built-in Functions
SELECT NEXT_DAY(SYSDATE, ’MONDAY’) FROM DUAL; Group Functions
Scalar Functions
NEXT_DAY(SYSDATE, ’MONDAY’) Date Functions
4.12
Built-in Functions
Numeric Functions
Chittaranjan Pradhan
Employee Table
ABS(n) Built-in Functions
Group Functions
It returns the absolute value of n
Scalar Functions
Date Functions
SELECT ABS(5), ABS(-100) FROM DUAL; Numeric Functions
Character Functions
Conversion Functions
ABS(5) ABS(-100) Misc. Functions
5 100
CEIL(n)
This returns the smallest integer greater than or equal to the
given value
4.13
Built-in Functions
Numeric Functions...
Chittaranjan Pradhan
DUAL Table
FLOOR(n) Employee Table
This returns the largest integer less than or equal to the given Built-in Functions
Group Functions
value
Scalar Functions
Date Functions
SELECT FLOOR(-5.2), FLOOR(5.7) FROM DUAL; Numeric Functions
Character Functions
EXP(n)
It returns the exponent e raised to power n
4.14
Built-in Functions
Numeric Functions...
Chittaranjan Pradhan
LN(n)
It returns the natural logarithm of n DUAL Table
Employee Table
Group Functions
LN(2)
0.693147181 Scalar Functions
Date Functions
Numeric Functions
Character Functions
LOG(b, n) Conversion Functions
Misc. Functions
It returns logb n value
MOD(n, m)
It returns the integer remainder of n/m
POWER(m, n)
DUAL Table
Built-in Functions
Scalar Functions
POWER(4,3) Date Functions
Numeric Functions
64 Character Functions
Conversion Functions
Misc. Functions
SQRT(n)
It returns the square root of the number n
4.16
Built-in Functions
Numeric Functions...
Chittaranjan Pradhan
DUAL Table
ROUND(m, [n]) Employee Table
Group Functions
TRUNC(m, n)
It returns the truncated value of m up to n positions
4.17
Built-in Functions
Numeric Functions...
Chittaranjan Pradhan
SIGN(n)
DUAL Table
It returns the sign of number n: -1 for negative, 0 for zero, 1 for Employee Table
Group Functions
SIN(n)
It returns sine of n, where n is in radian
Employee Table
CHR(n)
Built-in Functions
Scalar Functions
CONCAT(s1, s2)
It joins the first string to the second string. It is similar to the ||
operator
4.19
Built-in Functions
Character Functions...
Chittaranjan Pradhan
Employee Table
It pads the string s with the character c to the left to a total Built-in Functions
width of n Group Functions
Scalar Functions
SELECT LPAD(’ORACLE’,10,’*’) FROM DUAL; Date Functions
Numeric Functions
RPAD(s, n, c)
It pads the string s with the character c to the right to a total
width of n
SELECT RPAD(’ORACLE’,10,’*’) FROM DUAL;
RPAD(’ORACLE’,10,’*’)
ORACLE****
4.20
Built-in Functions
Character Functions...
Chittaranjan Pradhan
Employee Table
It returns the string with capitalization of the first letter in each Built-in Functions
word Group Functions
Scalar Functions
SELECT INITCAP(’HELLO’) FROM DUAL; Date Functions
Numeric Functions
Character Functions
INITCAP(’HELLO’) Conversion Functions
4.21
Built-in Functions
Character Functions...
Chittaranjan Pradhan
UPPER(s)
DUAL Table
Built-in Functions
Scalar Functions
UPPER(’HeLLo’) Date Functions
HELLO Numeric Functions
Character Functions
Conversion Functions
Misc. Functions
LTRIM(s, c)
It trims the string s from the left when the characters specified,
c, is present in s
DUAL Table
REPLACE(s, s1, s2) Employee Table
Group Functions
SUBSTR(s, n, m)
It returns a substring, starting at character position n, and
returns m number of characters
SELECT SUBSTR(’DATABASE’,3,2) FROM DUAL;
SUBSTR(’DATABASE’,3,2)
TA
4.23
Built-in Functions
Character Functions...
Chittaranjan Pradhan
DUAL Table
LENGTH(s)
Employee Table
Group Functions
SOUNDEX(s)
It compares words that are spell differently, but sound alike
4.24
Built-in Functions
Conversion Functions
Chittaranjan Pradhan
Employee Table
TO_NUMBER(char [,format]) Built-in Functions
Group Functions
It converts a character value with valid digits to a number using
Scalar Functions
the given format Date Functions
Numeric Functions
Character Functions
SELECT SUM(SAL) FROM EMP; Conversion Functions
TO_DATE(char [,format])
It converts a character value to date value based on the format
provided
4.25
Built-in Functions
Conversion Functions...
Chittaranjan Pradhan
DUAL Table
Employee Table
TO_CHAR(number [,format])
Built-in Functions
provided. 0 is used for compulsory purpose and 9 is used for Scalar Functions
Date Functions
optional purpose Numeric Functions
Character Functions
Conversion Functions
TO_CHAR(17145,’$999,999’)
$17,145
4.26
Built-in Functions
Conversion Functions...
Chittaranjan Pradhan
TO_CHAR(date [,format])
DUAL Table
It converts a date to a VARCHAR value based on the format Employee Table
provided Built-in Functions
Group Functions
SELECT TO_CHAR(HIREDATE,’MONTH DD, YYYY’) FROM Scalar Functions
EMP WHERE EMP_NO=7566; Date Functions
Numeric Functions
Character Functions
TO_CHAR(HIREDATE,’MONTH DD, YYYY’) Conversion Functions
Group Functions
WHERE DEPT_NO=10;
Scalar Functions
Date Functions
TO_CHAR(HIREDATE,’DDSP-MON-YY’) Numeric Functions
NINE-JUN-81 Character Functions
SEVENTEEN-NOV-81 Conversion Functions
Misc. Functions
TWENTY-THREE-JAN-82
Built-in Functions
It converts a NULL value to an actual value supplied as an Group Functions
argument. For numerical values, it accepts 0; whereas for Scalar Functions
character values, it accepts a fixed string Date Functions
Numeric Functions
Character Functions
COMMISSIONFROM EMP;
SELECT E_NAME, SALARY+NVL(COMMISSION, 0) Total
SalaryFROM EMP;
NVL2(column, notnullvalue, nullvalue)
It checks for NULL as well as not NULL values. If the column
has a not NULL value, the second parameter is displayed. If
the column has a NULL value, the third parameter is displayed