Functions
Using Single row functions to customize output
Oracle SQL supplies a rich library of in-built functions which can be employed for
various tasks. The essential capabilities of a functions can be the case conversion
of strings, in-string or substring operations, mathematical computations on numeric
data, and date operations on date type values. SQL Functions optionally take
arguments from the user and mandatorily return a value.
On a broader category, there are two types of functions :-
Single Row functions - Single row functions are the one who work on single row
and return one output per row. For example, length and case conversion functions
are single row functions.
Multiple Row functions - Multi row functions work upon group of rows and return
one result for the complete set of rows. They are also known as Group Functions.
Single row functions
Single row functions can be character functions, numeric functions, date functions,
and conversion functions. Note that these functions are used to manipulate data
items. These functions require one or more input arguments and operate on each
row, thereby returning one output value for each row. Argument can be a column,
literal or an expression. Single row functions can be used in SELECT statement,
WHERE and ORDER BY clause. Single row functions can be -
General functions - Usually contains NULL handling functions. The
functions under the category are NVL, NVL2, NULLIF, COALESCE, CASE,
DECODE.
Case Conversion functions - Accepts character input and returns a
character value. Functions under the category are UPPER, LOWER and
INITCAP.
o UPPER function converts a string to upper case.
o LOWER function converts a string to lower case.
o INITCAP function converts only the initial alphabets of a string to upper
case.
Character functions - Accepts character input and returns number or
character value. Functions under the category are CONCAT, LENGTH,
SUBSTR, INSTR, LPAD, RPAD, TRIM and REPLACE.
o CONCAT function concatenates two string values.
o LENGTH function returns the length of the input string.
o SUBSTR function returns a portion of a string from a given start point
to an end point.
Syntax:
SUBSTR(string,starting_postion,howmany_char_from_point)
o INSTR function returns numeric position of a character or a string in a
given string.
Syntax : INSTR(string,
’to which char uwant find the position’,
starting_postion,
which occurrence);
o LPAD and RPAD functions pad the given string upto a specific length
with a given character.
o TRIM function trims the string input from the start or end.
o REPLACE function replaces characters from the input string with a
given character.
Date functions - Date arithmetic operations return date or numeric values.
Functions under the category are MONTHS_BETWEEN, ADD_MONTHS,
NEXT_DAY, LAST_DAY, ROUND and TRUNC.
o MONTHS_BETWEEN function returns the count of months between
the two dates.
o ADD_MONTHS function add 'n' number of months to an input date.
o NEXT_DAY function returns the next day of the date specified.
o LAST_DAY function returns last day of the month of the input date.
o ROUND and TRUNC functions are used to round and truncates the
date value.
Number functions - Accepts numeric input and returns numeric values.
Functions under the category are ROUND, TRUNC, and MOD.
o ROUND and TRUNC functions are used to round and truncate the
number value.
o MOD is used to return the remainder of the division operation between
two numbers.
Illustrations
General functions
The SELECT query below demonstrates the use of NVL function.
SELECT first_name,last_name, salary, NVL (commission_pct,0)
FROM employees
WHERE rownum<5;
FIRST_NAME LAST_NAME SALARY
NVL(COMMISSION_PCT,0)
-----------------------------------------------------------------
-----------
StevenKing240000
NeenaKochhar170000
LexDeHaan170000
AlexanderHunold90000
Case Conversion functions
The SELECT query below demonstrates the use of case conversion functions.
SELECT UPPER (first_name), INITCAP (last_name), LOWER (job_id)
FROM employees
WHERE rownum<5;
`
UPPER(FIRST_NAME) INITCAP(LAST_NAME) LOWER(JOB_
-------------------------------------------------------
STEVEN Kingad_pres
NEENA Kochharad_vp
LEX DeHaanad_vp
ALEXANDER Hunoldit_prog
Character functions
The SELECT query below demonstrates the use of CONCAT function to
concatenate two string values.
SELECT CONCAT (first_name,last_name)
FROM employees
WHERE rownum<5;
CONCAT(FIRST_NAME,LAST_NAME)
--------------------------------
EllenAbel
SundarAnde
MozheAtkinson
DavidAustin
The SELECT query below demonstrates the use of SUBSTR and INSTR functions.
SUBSTR function returns the portion of input string from 1st position to 5th position.
INSTR function returns the numeric position of character 'a' in the first name.
SELECT SUBSTR (first_name,1,5), INSTR (first_name,'a')
FROM employees
WHERE rownum<5;
SUBST INSTR(FIRST_NAME,'A')
--------------------------
Ellen0
Sunda5
Mozhe0
David2
The SELECT query below demonstrates the usage of LPAD and RPAD to pretty
print the employee and job information.
SELECT RPAD(first_name,10,'_')||LPAD (job_id,15,'_')
FROM employees
WHERE rownum<5;
RPAD(FIRST_NAME,10,'_')||
-------------------------
Steven____________AD_PRES
Neena_______________AD_VP
Lex_________________AD_VP
Alexander_________IT_PROG
Number functions
The SELECT query below demonstrates the use of ROUND and TRUNC functions.
SELECT ROUND (1372.472,1)
FROM dual;
ROUND(1372.472,1)
-----------------
1372.5
SELECT TRUNC (72183,-2)
FROM dual;
TRUNC(72183,-2)
---------------
72100
Date arithmetic operations
The SELECT query below shows a date arithmetic function where difference of
employee hire date and sysdate is done.
SELECT employee_id,(sysdate-hire_date)Employment_days
FROM employees
WHERE rownum<5;
EMPLOYEE_ID EMPLOYMENT_DAYS
--------------------------
1003698.61877
1012871.61877
1024583.61877
1032767.61877
Date functions
The SELECT query below demonstrates the use of MONTHS_BETWEEN,
ADD_MONTHS, NEXT_DAY and LAST_DAY functions.
SELECT employee_id, MONTHS_BETWEEN
(sysdate,hire_date)Employment_months
FROM employees
WHERE rownum<5;
EMPLOYEE_ID EMPLOYMENT_MONTHS
----------------------------
100121.504216
10194.3751837
102150.633248
10390.9558289
SELECT ADD_MONTHS (sysdate,5), NEXT_DAY (sysdate), LAST_DAY
(sysdate)
FROM dual;
ADD_MONTH NEXT_DAY( LAST_DAY(
---------------------------
01-JAN-1405-AUG-1331-AUG-13
Functions
Number Functions
String Functions
Date Functions
Conversion Functions
General Functions
Aggrigate Functions
1) Number Functions
a) Power ( M, N )
Syntax : select power( 25, 2 ) from dual;
DUAL :
It is a dummy table which is provided by Oracle engine.
It has only one column which is associated with Varchar data type.
b) Sqrt ( M )
Syntax : select sqrt( 625 ) from dual;
c) Mod ( M, N )
Syntax : select mod( 5, 2 ) from dual;
d) Ascii ( C )
Syntax : select ascii( ‘a’ ) from dual;
e) Ceil ( M )
It displays the next highest value
Syntax : select ceil ( 12.45 ) from dual.
f) Floor ( M )
It displays the next lowest value
Syntax : select floor ( 13.65) from dual;
g) Round ( M, N)
It rounds the value up to given number of position. That is if last
eliminating value is >=5
then it simply add one value to the left adjacent value.
It check the condition.
Syntax : select round ( 15.2345, 2 ) from dual;
h) Trunc( M, N )
It work similar to that of round, but it won’t check the condition.
Syntax : select trunk ( 12.567, 2 ) from dual;
2) Sting Functions
a) Length ( S )
It is used to display the number of characters in a given string.
Syntax : select length( ‘ebs’ ) from dual;
b) Reverse ( S )
It is used to reverse the given string.
Syntax ; select reverse ( ‘ebs’ ) from dual;
c) Upper ( S)
It is used to convert the string into upper characters.
Syntax : select upper( ‘ebs’ ) from dual;
d) Lower ( S )
It is used to convert the string into lower characters.
Syntax : select lower ( ‘EBS’ ) from dual;
e) Initcap( S )
It is used to convert the first character into upper character in a given string.
Syntax : select initcap ( ‘business’ ) from dual;
f) Concat( S1, S2 )
It is used to merge the two strings. And we have to use ‘||’ symbol while merge
the twostrings.
Syntax : select concat ( ‘ebs’, ’solutions’ ) from dual;
Syntax : select ‘ebs’ || ‘business’ || ‘solutions’ from dual;
g) Ltrim( S, C )
It is used to remove the character from left end of the given string, if the
character isfound.
Syntax : select ltrim ( ‘ebsebs’ , ‘e’ ) from dual;
h) Rtrim( S, C )
It is used to remove the character from right end of the given string, if the
character isfound.
Syntax : select rtrim ( ‘ebsess’ , ‘s’ ) from dual;
i) Trim
It is used to remove the characters from both sides in a given string.
Syntax : select trim ( ‘e’ from ‘eebse’ ) from dual;
j) Lpad
It is used to add the character from left end.
Syntax : select lpad ( ‘ebs’, 5 , ‘&’ ) from dual;
k) Rpad
It is used to add the character from rightend.
Syntax : select rpad ( ‘ebs’, 7 , ‘&’ ) from dual;
l) Translate ( S, C, C )
It is used to translate the character wise in a given string, if the character is
found.
It is not possible to translate entire string.
Syntax : select translate ( ‘welcome’ , ‘w’ , ‘t’) from dual;
m) Replace ( S, S ,S )
It is used to replace entire string.
It is not possible to replace more than one string.
Syntax : select replace ( ‘e business solutions’, ‘business’, ‘ebs’ ) from
dual;
n) Decode ( Column, Condition, Do1,…………….. Column)
It is used replace more than one string.
It works like as a if condition but it does not allow the relational operators.
Syntax : select job, decode ( job, ‘manager’, ‘mgr’, ‘clerk’, ‘clk’,
‘salesman’, ‘sls’, job )from dual;
o) Case ( when condition then result else default value )
It is used to replace more than one string by using relational operator.
Syntax : select case when deptno=10 and job=’MANAGER’ then ‘mgr’ else job
end jfrom emp;
p) Substr( S, M, N )
It is used to display the set of characters from a given string.
S = String
M = Position
N = No of Characters
Syntax : select substr ( ‘welcome’, 1,3 ) from dual;
q) Instr( S, C, M, N )
It is used to display the position number of a given character.
S = String
C = Character
M = Position
N = Occurance
Syntax : select instr ( ‘welcome’, ‘e’, 1, 1 ) from dual;
3) Data Functions
a) Sysdate :
It is used to display the system date.
Syntax : select sysdate from dual;
b) Current_Date :
It is used to display the next day.
Syntax : select current_date from dual;
c) Add_Months :
It is used to add or substract number of months for a given date.
Syntax : select add_months( sysdate, 1) from dual;
d) Months_Between( Date1, Date2 ):
It is used to display the number of months between two dates
Syntax : select months_between ( sysdate, hiredate ) from emp;
e) Next_Day( Date, ‘format’ )
It is used to display the next day date based on the format.
Syntax : select next_day ( sysdate, ‘sun’ ) from dual;
f) Last_Day( Date )
It is used to display the last day of the given month.
Syntax : select last_day ( sysdate ) from dual;
Date Formats :
D => Number of day in the week
DD => Number of day in the month
DDD => Number of day in the year
DY => First 3 Characters of the day - SUN
Dy => First 3 Characters of the day - Sun
dy => First 3 Characters of the day - sun
DAY => Complete Characters of the day
Day => Complete Characters of the day
day => Complete Characters of the day
MM => Number of the month in the year.
MON => First 3 Characters of the month
Mon => First 3 Characters of the month
mon => First 3 Characters of the month
MONTH => Complete Charaters of the month
Month => Complete Charaters of the month
month => Complete Charaters of the month
Y => Last digit of the year
YY => Last two digits of the year
YYYY => Last three digits of the year
YYYY => Four digits of the year
YEAR => Year in the character format.
HH => An hour of the day
HH24 => 24 Hours format.
MI =>Minits of the Hour
SS => Seconds of the minute.
SSSS => Seconds since starting of the day
FS => Fraction of Seconds
W => Week ot the month
WW => Week of the year
Q => Quarter of the year
RRRR
4) Conversion Functions
a) To_Char( Date, ‘format’ )
It is used to convert system format in to user format
Syntax : select to_char ( sysdate, ‘day’ ) from dual;
b) To_Date( ‘C’, ‘format’ )
It is used to convert user format into system format
Syntax : select to_date ( ‘21’, ‘DD’ ) from dual;
Select to_date( ‘december’, ‘MM’ ) from dual;
c) To_Number
It is used to translate a value of char or varchar data type to number
format.
Syntax : select to_number ( ‘20’ ) from dual;
5) General Funtions
a) User &Uid
Select user,uid from dual;
b) Greatest & Least
Select greatest ( 1,2,3 ), least ( 1, 2, 3 ) from dual;
c) NVL ( Col1, Val )
It is used to handle the null values
It work like as a if condition
Syntax : select sal, comm,sal+nvl(comm, 0) from emp;
d) NVL2 ( Col1, Val1, Val2 )
It is a advanced of nvl
It work like as a if then else condition
Syntax : select sal, comm, nvl2 ( comm, 0, 100 ) from emp;
6) Aggregate Functions
a) Min
Syntax : select min ( sal ) from emp;
select min ( sal ),depno,mgr from emp group by deptno,mgr,sql;
b) Max
Syntax : select max ( sal ) from emp;
c) Avg
Syntax : select avg ( sal ) from emp;
d) Sum
Syntax : select sum ( sal ) from emp;
e) Count ( * )
It is used to count of the all records from a table
Syntax : select count( * ) from emp;
f) Count ( column )
It is used to count the given column values
Syntax : select count ( empno ) from emp;