Inbuilt FUNCTIONS
Inbuilt FUNCTIONS
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;
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
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
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;
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;
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;