SQL Functions
Types of Functions
• Numeric Functions
– Single value functions
– List value functions
– Group Value Functions
• Character Functions
• Date functions
DUAL Table
What is a DUAL Table in Oracle?
* This is a single row and single column dummy table provided
by Oracle
* This is used to perform mathematical calculations without
using a table.
Select * from DUAL
DUMMY
-------
X
Select 777 * 888 from Dual
• Output:
• 777 * 888
---------
689976
Numeric Functions
Function Name Examples Return Value
ABS (1) 1
ABS (x)
ABS (-1) -1
CEIL (2.83) 3
CEIL (x) CEIL (2.49) 3
CEIL (-1.6) -1
FLOOR (2.83) 2
FLOOR (x) FLOOR (2.49) 2
FLOOR (-1.6) -2
ROUND (125.456, 1) 125.4
ROUND (x, y) ROUND (125.456, 0) 125
ROUND (124.456, -1) 120
TRUNC (140.234, 2) 140.23
TRUNC (x, y) TRUNC (-54, 1) 54
TRUNC (5.7) 5
TRUNC (142, -1) 140
Character Functions
Function Name Return Value
All the letters in 'string_value' is
LOWER (string_value)
converted to lowercase.
All the letters in 'string_value' is
UPPER (string_value)
converted to uppercase.
All the letters in 'string_value' is
INITCAP (string_value)
converted to mixed case.
All occurrences of 'trim_text'is removed
LTRIM (string_value, trim_text)
from the left of 'string_value'.
All occurrences of 'trim_text' is removed
RTRIM (string_value, trim_text)
from the right of 'string_value' .
All occurrences of 'trim_text' from the left
TRIM (trim_text FROM string_value) and right of 'string_value' ,'trim_text' can
also be only one character long .
Function Name Return Value
Returns 'n' number of
SUBSTR (string_value, m, n) characters from 'string_value'
starting from the 'm' position.
Number of characters in
LENGTH (string_value)
'string_value' in returned.
Returns 'string_value' left-
padded with 'pad_value' . The
LPAD (string_value, n, pad_value)
length of the whole string will
be of 'n' characters.
Returns 'string_value' right-
padded with 'pad_value' . The
RPAD (string_value, n, pad_value)
length of the whole string will
be of 'n' characters.
Function Name Examples Return Value
LOWER(string_value) LOWER('Good Morning') good morning
UPPER(string_value) UPPER('Good Morning') GOOD MORNING
INITCAP(string_value) INITCAP('GOOD MORNING') Good Morning
LTRIM(string_value, trim_text) LTRIM ('Good Morning', 'Good) Morning
RTRIM ('Good Morning', '
RTRIM (string_value, trim_text) Good
Morning')
TRIM (trim_text ,string_value) TRIM ('o' , 'Good Morning') GdMrning
SUBSTR (string_value, m, n) SUBSTR ('Good Morning', 6, 7) Morning
LENGTH (string_value) LENGTH ('Good Morning') 12
LPAD (string_value, n, pad_value) LPAD ('Good', 6, '*') **Good
RPAD (string_value, n, pad_value) RPAD ('Good', 6, '*') Good**
Date Functions
Function Name Return Value
ADD_MONTHS (date, n) Returns a date value after adding 'n' months to the date 'x'.
MONTHS_BETWEEN (x1, x2) Returns the number of months between dates x1 and x2.
Returns the date 'x' rounded off to the nearest century, year,
ROUND (x, date_format) month, date, hour, minute, or second as specified by the
'date_format'.
Returns the date 'x' lesser than or equal to the nearest century,
TRUNC (x, date_format) year, month, date, hour, minute, or second as specified by the
'date_format'.
Returns the next date of the 'week_day' on or after the date 'x'
NEXT_DAY (x, week_day)
occurs.
It is used to determine the number of days remaining in a
LAST_DAY (x)
month from the date 'x' specified.
SYSDATE Returns the systems current date and time.
Returns the date and time in zone2 if date 'x' represents the
NEW_TIME (x, zone1, zone2)
time in zone1.
Function Name Examples Return Value
ADD_MONTHS ('16-Sep-
ADD_MONTHS ( ) 16-Dec-81
81', 3)
MONTHS_BETW MONTHS_BETWEEN ('16-
3
EEN( ) Sep-81', '16-Dec-81')
NEXT_DAY ('01-Jun-08',
NEXT_DAY( ) 04-JUN-08
'Wednesday')
LAST_DAY( ) LAST_DAY ('01-Jun-08') 30-Jun-08
NEW_TIME ('01-Jun-08',
NEW_TIME( ) 31-May-08
'IST', 'EST')
Standard Time Zone
• AST, ADT: Atlantic Standard or Daylight Time
• BST, BDT: Bering Standard or Daylight Time
• CST, CDT: Central Standard or Daylight Time
• EST, EDT: Eastern Standard or Daylight Time
• GMT: Greenwich Mean Time
• HST, HDT: Alaska-Hawaii Standard Time or Daylight Time.
• MST, MDT: Mountain Standard or Daylight Time
• NST: Newfoundland Standard Time
• PST, PDT: Pacific Standard or Daylight Time
• YST, YDT: Yukon Standard or Daylight Time
SELECT NEW_TIME(TO_DATE('11-10-09 01:23:45', 'MM-DD-YY
HH24:MI:SS'), 'AST', 'PST') from dual
Conversion Functions:
Function Name Return Value
Converts Numeric and Date values to a character
TO_CHAR (x [,y]) string value. It cannot be used for calculations
since it is a string value.
Converts a valid Numeric and Character values to
TO_DATE (x [, date_format]) a Date value. Date is formatted to the format
specified by 'date_format'.
If 'x' is NULL, replace it with 'y'. 'x' and 'y' must
NVL (x, y)
be of the same datatype.
DECODE (a, b, c, d, e, Checks the value of 'a', if a = b, then returns 'c'. If
default_value) a = d, then returns 'e'. Else, returns default_value.
Function Name Examples Return Value
TO_CHAR (3000, '$9999')
$3000
TO_CHAR () TO_CHAR (SYSDATE, Monday, June 2008
'Day, Month YYYY')
TO_DATE () TO_DATE ('01-Jun-08') 01-Jun-08
NVL () NVL (null, 1)
Aggregate Functions
returns the smallest value in
MIN
a given column
returns the largest value in a
MAX
given column
returns the sum of the numeric
SUM
values in a given column
returns the average value of a
AVG
given column
returns the total number of values
COUNT
in a given column
returns the number of rows in a
COUNT(*)
table
Aggregate Functions
Average salary for all employee
SELECT AVG(salary) FROM employee;
Average salary for all employee whose title is
equal to 'Programmer‘
SELECT AVG(salary)
FROM employee
WHERE title = 'Programmer';
Aggregate Functions
To display the number of
Employees
SELECT Count(*) FROM employee;
GROUP BY clause
• The GROUP BY clause will gather all of the
rows together that contain data in the
specified column(s) and will allow aggregate
functions to be performed on the one or more
columns.
• Retrieve a list of the highest paid salaries
in each dept:
SELECT max(salary), dept
FROM employee
GROUP BY dept;
Having clause
• Retrieve a list of the highest
paid salaries in each dept having
salry greater than 20000:
SELECT max(salary), dept
FROM employee
GROUP BY dept having
max(salary)>20000;