0% found this document useful (0 votes)
60 views27 pages

Functions: String Function

The document discusses various SQL functions. It describes two types of functions - predefined functions provided by Oracle and user-defined functions created by the user. It provides examples of string, numeric, date, conversion and other special functions. Some key functions discussed include UPPER, LOWER, LENGTH, REPLACE, SUBSTR, INSTR, NVL, NVL2, TRANSLATE, WM_CONCAT and more.

Uploaded by

shilpa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
60 views27 pages

Functions: String Function

The document discusses various SQL functions. It describes two types of functions - predefined functions provided by Oracle and user-defined functions created by the user. It provides examples of string, numeric, date, conversion and other special functions. Some key functions discussed include UPPER, LOWER, LENGTH, REPLACE, SUBSTR, INSTR, NVL, NVL2, TRANSLATE, WM_CONCAT and more.

Uploaded by

shilpa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 27

FUNCTIONS

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)

b) User-defined functions: used only in PL/SQL (defined by the user)

List of PRE-defined Functions

1. String (character) 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

UPPER converts into uppercase.

LOWER converts into lowercase.

INITCAP (initial CAP) the first letter of every word will be convert into uppercase and rest

of letters converted into lowercase.

LENGTH returns the number of character

REVERSE it reverse the given string.

REPLACE it replace the OLD value with new value

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

LTRIM- It trims of a given character from the LHS of a string.

RTRIM- It Trims of a given character from the RHS of a string.

TRIM- It trims of a given character from both LHS and RHS of a string.

Page 3
// by default trim removes the spaces.

CONCAT

It concatenates any 2 values. Only 2 parameter can be given in this function

We have option || which will work same as CONCAT function

Page 4
LPAD and RPAD

They add a given character to the given size in LHS and RHS respective. This function

is helpful for justification.

// without RPAD function

Page 5
//with RPAD function

Sub-string function

It extracts certain given number of character from a string

Extraction: positive left to right

Negative right to left

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)

Number of extracting character (only positive)

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 the position of a given string.

 It returns ‘zero’ if it character is not present else returns the position number.

INSTR( ename, ‘A’ , 1 , 1)

Nth occurrence

Starting position

Character to be searched

Given string

Extraction: positive left to right

Negative right to left

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

Select INSTR(‘CORPORATE FLORS’, ‘OR’, -3, 2) from dual = 5

Select INSTR(‘CORPORATE FLORS’, ‘OR’, -3, 3) from dual =2

-List the product code which are having letter 'P' from the second character onwards

PCODE
----------
P101A
P102B
P101Q
P102P
Q102P

Select pcode from products where instr(pcode,’p’,2)>0

Display the employees name who are having two "L"


Display the employees name who are having exactly one "L"
Having 2 ‘L’

Having Exactly 1 ‘L’

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

Write a query interchange the Player names

NAME
-Display the first name only
-Display the second name only
-Display the last name

Page 10
Null function

NVL : It provides substitute value from nulls

Page 11
NVL2: It provides Substitute value for both null as well as not null

For null

For not null

Comparison between NVL and NVL2

NVL NVL2
 Provides substitute value for NULL.  Provides substitute value for NULL &

NOT NULL.

 Conversion is required.  Conversion is not required.

 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.

Displaying NULL due to


sal and comm as same
value

COALESCE function

It returns the first not null values

coalesce (col1, col2, col3, ....)

if col1 is not null then

display col1.

if col1 is null AND col2 is NOT NULL then

display col2.

if col1 is null AND col2 NULL AND col3 is not null then

display col3.

if all are NULL it returns NULL itself.

NOTE: if comm is null and mgr is null and sal is not null then prints sal.

if comm is not null then prints comm.

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.

CHR: It returns the character of the given ASCII value.

To find the ASCII value of middle character from ename

Ename=blake (length(ename)/2)+1=AKE ASCII(AKE) is 65

Scott length(scott)/2=2.5

=2+1 (1 is added due to oracle takes whole number)

=3

Page 14
Chr(0)-------> space Chr(10)-------->next line

USER: just returns username which is logged in.

UID: just returns the user id which is logged in.

TRANSLATE: It does character by character replacement.

O-1 , R-2 , P-2 , S-4 , B-I , V-F , L-EMPTY

O-1 , R-2 , P-2 , S-4 , B-I , V-F , L-NULL

Page 15
Only 1st character will consider

WM_CONCAT: It will display a column data in row format and default delimiter is ‘,’

SYS_CONTEXT: It helps to get the system properties / server properties.

Userenv is constant

Parameters:

Host- serever name

IP_address – IP address

Db_name- database name

Lang- language

………

Numeric Function :

 ABS(n) : returns the absult value

 SQRT(n) :Square root of the given number

 power(x,y) : returns 'X' power 'Y' value

Page 16
 MOD(x,y) : returns the remainder when 'X' is divided by 'Y'

 exp(x) : returns the 'e' power 'x' values(e=2.71)

 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 & Least

 Greatest returns the highest value among set of given no. of value

 least returns the lowest value

Page 18
MAX GREATEST

 Only one Parameter  'n' Parameter /colums

 highest value in a single column across  highest value for every row among

all the rows given columns

 aggregate function  non-aggregate(scalar) function

ROUND &TRUNC

 Round will round off the given no. to the nearest value

 trunc will trucate(remove) the value

Page 19
Date function

SYSDATE: It will return the system date


SYSTIMESTAMP: It returns date, time, timezone

LAST_DAY: It returns the date of the last day of the month

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

Next 2 day of current day

Next 2 hours of current day

Next 1200 min of current day

Next 1200 sec of current day

Page 21
Add_months: It adds or substract ‘n’ months to the given date.

To get next 2 months of same date

To get next year of same date

To get last year of same date

Months_between: It returns the difference in two date in terms of months.

In terms of months

Page 22
In terms of years

In terms of days

Conversion function

Helps to convert from one data type to another datatype

a. to_char : Converts from number or date to a character

b. to_date : Converts a Character to a date

c. to_number : Converts a Character to a number if it is a valid number

Formats:

dd - day of the month(1-31)

ddd -day of the year(1-366)


d - day of the week(1-7) (1-sun, 7-sat)
mm - month number(1-12)
yyyy - year in 4 digit format
yy - year in 2 digit format
hh - hours(12 hr format)
hh24 - hours(24 hr format)
mi - minute
ss - second
am - Suffixes AM/PM at the end of the time

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

List the employees who have not joined on week-ends

Display the no. of employees joined in each month

Page 25
Display the no. of employees joined in each year

Display the no. of employees joined in each day

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

You might also like