Functions: String Function
Functions: String Function
A function is a reusable program that should return a value, they are classified into 2
types
a) Pre-defined functions: used in SQL and PL/SQL (given by oracle / in-built functions)
2. Numeric functions
3. Date functions
4. Conversion functions
5. Special functions
6. Aggregate functions
7. Analytical functions
8. Null functions
9. Trigonometric functions
String Function
INITCAP (initial CAP) the first letter of every word will be convert into uppercase and rest
Page 1
Page 2
// nested functions—Function within a function is called nested functions, we can use upto
32 nested functions//
TRIM Concepts
LTRIM, RTRIM, TRIM
TRIM- It trims of a given character from both LHS and RHS of a string.
Page 3
// by default trim removes the spaces.
CONCAT
Page 4
LPAD and RPAD
They add a given character to the given size in LHS and RHS respective. This function
Page 5
//with RPAD function
Sub-string function
1 2 3 4 5 6 7 8 9
S Q L P L S Q L
-9 -8 -7 -6 -5 -4 -3 -2 -1
SUBSTR(‘SQL PLSQL’, 1 , 3)
Position
String
Page 6
name=ORACLE SQL
Select SUBSTR(name,1,3) from dual ORA
Select SUBSTR(name,2,7) from dual RACLE S
Select SUBSTR(name,5,1) from dual L
Select SUBSTR(name,5) from dual LE SQL
Select SUBSTR(name,-6,2) from dual LE
Select SUBSTR(name,-3) from dual SQL
Select REVERSE(SUBSTR(name,-3)) from dual LQS
Display the 1st 3 charater and remaining charater of dname separated by ‘-‘
Display all the emps whose job ends with ‘MAN’ (with like and without like)
//with using like operator
Page 7
//without using like operator
INSTRING
It returns ‘zero’ if it character is not present else returns the position number.
Nth occurrence
Starting position
Character to be searched
Given string
1 2 3 4 5 6 7 8 9
S Q L P L S Q L
-9 -8 -7 -6 -5 -4 -3 -2 -1
Page 8
str=SQL PLSQL
Select INSTR(str, ‘L’, 3 ,1) from dual 3
Select INSTR(str, ‘L’, 4 ,1) from dual 6
Select INSTR(str, ‘L’, 5 ,2) from dual 9
Select INSTR(str, ‘L’, 5 ,3) from dual 0
Select INSTR(str, ‘L’) from dual 3 By default it is 1,1
Select INSTR(str, ‘L’, -3 ,1) from dual 6
-List the product code which are having letter 'P' from the second character onwards
PCODE
----------
P101A
P102B
P101Q
P102P
Q102P
Page 9
1)select * from mailid
MAIL-ID
--------------
test@yahoo.com
scott@gmail.com
turner@xyz.com
EMPNAME MAILNAME
---------------- ------------------
test yahoo.com
scott gmail.com
turner xyz.com
NAME
-Display the first name only
-Display the second name only
-Display the last name
Page 10
Null function
Page 11
NVL2: It provides Substitute value for both null as well as not null
For null
NVL NVL2
Provides substitute value for NULL. Provides substitute value for NULL &
NOT NULL.
Can be used in SQL statements and Can be used only in SQL statements.
procedural statements.
Begin Begin
vt := nvl(var,0);--> works var := nvl2(vt,1,0); --> error.
end; end;
Begin Begin
select nvl(var,0) into vt from dual; --> works select nvl2(vt,1,0) into var from dual;
end; end;
Page 12
NULLIF function
It compares any two values & returns null if they are same else it returns first value
itself.
COALESCE function
display col1.
display col2.
if col1 is null AND col2 NULL AND col3 is not null then
display col3.
NOTE: if comm is null and mgr is null and sal is not null then prints sal.
Page 13
Query to display sal,comm & their status(status='same' if sal=comm else 'diff')
SPECIAL FUNCTION
ASCII: It returns the ASCII value of a given character.
Scott length(scott)/2=2.5
=3
Page 14
Chr(0)-------> space Chr(10)-------->next line
Page 15
Only 1st character will consider
WM_CONCAT: It will display a column data in row format and default delimiter is ‘,’
Userenv is constant
Parameters:
IP_address – IP address
Lang- language
………
Numeric Function :
Page 16
MOD(x,y) : returns the remainder when 'X' is divided by 'Y'
sign : it returns one for any +ve no. -1 for any -ve number &zero for zero
Page 17
CEIL & FLOOR: ceil returns the highest integer value where as Floor returns the lowest
integer value
Greatest returns the highest value among set of given no. of value
Page 18
MAX GREATEST
highest value in a single column across highest value for every row among
ROUND &TRUNC
Round will round off the given no. to the nearest value
Page 19
Date function
NEXT_DAY: It returns the date of next available day with respective given day and date.
Page 20
NLS----------> National language support
Date Arithmetic: It adds the day, hour, min, sec to the given date
Page 21
Add_months: It adds or substract ‘n’ months to the given date.
In terms of months
Page 22
In terms of years
In terms of days
Conversion function
Formats:
Page 23
q - quater(1-4)
w - week of the month(1-5)
ww - week of the year(1-52)
day - full day name(sunday)
dy - short day name(mon,tue.....)
month - full month name(june,july)
mon - short month name(jan,feb...)
To Spell
Select To_char(sysdate,’FMDDTHSP MONTH YYYYSP’) from dual;
Page 24
List the employees who have joined on week-ends
Page 25
Display the no. of employees joined in each year
Display the no. of employees joined in each month & for each year
Page 26
List the employees and their day of joining and sort the data according to the day(mon,thu,wed....sun)
Page 27