Professional Documents
Culture Documents
SQL 4
SQL 4
Group Functions
DUAL Table
1 DUAL Table Employee Table
Built-in Functions
Group Functions
2 Employee Table
Scalar Functions
Date Functions
Numeric Functions
4 Group Functions
5 Scalar Functions
Date Functions
Numeric Functions
Character Functions
Conversion Functions
Misc. Functions
4.2
Built-in Functions
DUAL Table
DUAL Table
Employee Table
Built-in Functions
DUAL Table
Group Functions
DUAL table is a small worktable, which consists of only one Scalar Functions
column DUMMY and a single row with value X of VARCHAR2 Date Functions
Numeric Functions
4.3
Built-in Functions
Employee Table
DUAL Table
Employee Table
Built-in Functions
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO Group Functions
4.4
Built-in Functions
Built-in Functions
DUAL Table
EmployeeTable
Built-in Functions
Built-in Functions
GroupFunctions
The built-in functions provide a powerful tool for the ScalarFunctions
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
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
COUNT(*)
14
4.6
Built-in Functions
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...
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
4.9
Built-in Functions
Date Functions
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
SYSDATE
21-JAN-13
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...
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...
NEXT_DAY(date, ’day’)
DUAL Table
It finds the next occurrence of a day from the given date Employee Table
Built-in Functions
Scalar Functions
NEXT_DAY(SYSDATE, ’MONDAY’) Date Functions
4.12
Built-in Functions
Numeric Functions
These functions take numeric values and return a numeric
value. The different functions in this category are: DUAL Table
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...
DUAL Table
FLOOR(n) Employee Table
Built-in Functions
This returns the largest integer less than or equal to the given
Group Functions
value
Scalar Functions
Date Functions
SELECT FLOOR(-5.2), FLOOR(5.7) FROM DUAL; Numeric Functions
Character Functions
Conversion Functions
FLOOR(-5.2) FLOOR(5.7)
Misc. Functions
-6 5
EXP(n)
It returns the exponent e raised to power n
4.14
Built-in Functions
Numeric Functions...
LN(n)
It returns the natural logarithm of n DUAL Table
Employee Table
Group Functions
LN(2)
Scalar Functions
0.693147181
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...
DUAL Table
ROUND(m, [n]) Employee Table
Built-in Functions
It returns m, rounded to n places to the right of a decimal point
Group Functions
Scalar Functions
SELECT ROUND(15.19,1), ROUND(15.19) FROM DUAL; Date Functions
Numeric Functions
TRUNC(m, n)
It returns the truncated value of m up to n positions
4.17
Built-in Functions
Numeric Functions...
SIGN(n)
DUAL Table
It returns the sign of number n: -1 for negative, 0 for zero, 1 for Employee Table
Built-in Functions
positive
Group Functions
Scalar Functions
SELECT SIGN(-8.5) FROM DUAL; Date Functions
Numeric Functions
SIGN(-8.5) Character Functions
Conversion Functions
-1 Misc. Functions
SIN(n)
It returns sine of n, where n is in radian
ScalarFunctions
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...
ScalarFunctions
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...
ScalarFunctions
SELECT INITCAP(’HELLO’) FROM DUAL; Date Functions
Numeric Functions
Character Functions
INITCAP(’HELLO’) Conversion Functions
4.21
Built-in Functions
Character Functions...
UPPER(s)
DUAL Table
Built-in Functions
ScalarFunctions
UPPER(’HeLLo’) Date 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
Built-in Functions
It returns the string s with the replacement of s2 in place of s1
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...
DUAL Table
LENGTH(s)
Employee Table
Group Functions
SOUNDEX(s)
It compares words that are spell differently, but sound alike
E_NAME
KING
4.24
Built-in Functions
Conversion Functions
Group Functions
It converts a character value with valid digits to a number using
ScalarFunctions
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...
DUAL Table
Employee Table
TO_CHAR(number [,format]) Built-in Functions
ScalarFunctions
provided. 0 is used for compulsory purpose and 9 is used for Date Functions
TO_CHAR(17145,’$999,999’)
$17,145
4.26
Built-in Functions
Conversion Functions...
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 ScalarFunctions
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; ScalarFunctions
Date Functions
TO_CHAR(HIREDATE,’DDSP-MON-YY’) Numeric Functions
NINE-JUN-81 Character Functions
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
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