SlideShare a Scribd company logo
Built-in SQL Functions
2
Type of Functions
 Character Functions
 returning character values
 returning numeric values
 Numeric Functions
 Date Functions
 Conversion Functions
 Group Functions
 Error Reporting
 Other Functions
3
Character Functions
Returning Character Values
 CHR
 CONCAT
 INITCAP
4
Character Functions
Returning Character Values
 LOWER
 LPAD
 LTRIM
 NLS_INITCAP
5
Character Functions
Returning Character Values
 NLS_LOWER
 NLS_UPPER
 NLSSORT
 REPLACE
 RPAD
6
Character Functions
Returning Character Values
 RTRIM
 SOUNDEX
 SUBSTR
 SUBSTRB
 TRANSLATE
 UPPER
7
Character Functions
Returning Numeric Values
 ASCII
 INSTR
 INSTRB
 LENGTH
 LENGTHB
8
Numeric Functions
 ABS
 ACOS
 ASIN
 ATAN
 ATAN2
9
Numeric Functions
 CEIL
 COS
 COSH
 EXP
 FLOOR
 LN
10
Numeric Functions
 LOG
 MOD
 POWER
 ROUND
 SIGN
 SIN
11
Numeric Functions
 SINH
 SQRT
 TAN
 TANH
 TRUNC
12
Date Functions
 ADD_MONTHS
 LAST_DAY
 MONTHS_BETWEEN
 NEW_TIME
 NEXT_DAY
 ROUND
 SYSDATE
 TRUNC
13
Conversion Functions
 CHARTOROWID
 CONVERT
 HEXTORAW
 RAWTOHEX
 ROWIDTOCHAR
14
Conversion Functions
 TO_CHAR
 TO_DATE
 TO_LABEL
 TO_MULTI_BYTE
 TO_NUMBER
 TO_SINGLE_BYTE
15
Group Functions
 AVG
 COUNT
 GLB
 LUB
16
Group Functions
 MAX
 MIN
 STDDEV
 SUM
 VARIANCE
17
Error Reporting Functions
 SQLCODE
 SQLERRM
18
Other Functions
 BFILENAME
 DECODE
 DUMP
 GREATEST
 GREATEST_LB
 LEAST
19
Other Functions
 LEAST_LB
 NVL
 UID
 USER
 USERENV
 VSIZE
20
Agenda
 Stored Procedures
 Functions
 Parameters
 Calling Stored Procedures &
Functions
 Examples
21
Stored Procedures
 Named PL/SQL blocks that
 Are stored in the database
 May have formal parameters
 Can return more than one value to the calling
program
 Can be called from

within other PL/SQL blocks as a PL/SQL statement by itself

SQL> prompt
22
PL/SQL Block vs. Stored
Procedures
DECLARE
-- variable declaration
BEGIN
-- required executable
EXCEPTION
-- exception handling
END;
/
CREATE OR REPLACE PROCEDURE X
[(formal_parameters)] AS[IS]
-- variable declaration
BEGIN
-- required executable
EXCEPTION
-- exception handling
END X;
/
Anonymous PL/SQL Block Stored Procedure
23
Parameters
 Parameters are optional
 MUST be given a data type, but must NOT be given a size
 Parameters have 3 modes
 IN

Read-only within procedure/function

Default mode (if mode is not explicitly specified)
 OUT

Has an initial value of NULL within the procedure/function

Ignores any values that the actual parameters have when the procedure/function is called

Can read from and write to
 IN OUT

Value of actual parameters are passed into procedure/function

Can read from and write to
24
Stored Procedure with
Parameters
CREATE OR REPLACE PROCEDURE X (
p_Parameter1 IN VARCHAR2,
p_Parameter2 IN NUMBER,
p_Parameter3 OUT VARCHAR2,
p_Parameter4 OUT NOCOPY NUMBER,
p_Parameter5 IN OUT NUMBER DEFAULT 1) AS
-- variable declaration
BEGIN
-- required executable
EXCEPTION
-- exception handling
END X;
/
25
set serveroutput on
CREATE OR REPLACE PROCEDURE BoatReservations(p_Color IN VARCHAR2) AS
CURSOR c_Reservations IS
SELECT s.sname, r.day, r.bid
FROM Sailor s, Reserve r, Boat b
WHERE r.sid = s.sid
AND r.bid = b.bid
AND b.color = p_Color;
v_Reservation c_Reservations%ROWTYPE;
BEGIN
OPEN c_Reservations;
FETCH c_Reservations INTO v_Reservation;
WHILE c_Reservations%FOUND LOOP
DBMS_OUTPUT.PUT_LINE(v_Reservation.sname||' '||v_Reservation.day||
'||v_Reservation.bid);
FETCH c_Reservations INTO v_Reservation;
END LOOP;
CLOSE c_Reservations;
END BoatReservations;
/
26
Functions
 Named PL/SQL blocks that
 Are stored in the database
 May have formal parameters
 MUST use the keyword RETURN to return only one value

RETURN passes control back to the calling program

Required for functions
 Can be called from

within other PL/SQL blocks as part of an expression

SQL> prompt
27
Stored Procedures vs. Functions
CREATE OR REPLACE PROCEDURE X
[(parameters)] AS
-- variable declaration
BEGIN
-- required executable
EXCEPTION
-- exception handling
END X;
/
CREATE OR REPLACE FUNCTION X
[(formal_parameters)] RETURN
return_type IS[AS]
-- variable declaration
BEGIN
-- required executable
-- required RETURN statement
RETURN Z;
EXCEPTION
-- exception handling
END X;
/
Stored Procedure Function
28
CREATE OR REPLACE FUNCTION NextBusinessDate1 (p_Date DATE)
RETURN DATE IS
-- Variable that will contain the day that corresponds to the date parameter
v_CurrentDay VARCHAR2(9);
-- Variable that will contain the computed date of the next business day
v_NextDate DATE;
BEGIN
/*First, determine the corresponding name of the day for the date parameter. It will be
used later
to determine the number of days by which the date should be incremented.*/
v_CurrentDay := UPPER(TRIM(TO_CHAR(p_Date, 'DAY')));
/*Based upon the name of the day and the business rule, calculate the next business
date*/
IF v_CurrentDay = 'FRIDAY' THEN
v_NextDate := p_Date + 3;
ELSIF v_CurrentDay = 'SATURDAY' THEN
v_NextDate := p_Date + 2;
ELSE
v_NextDate := p_Date + 1;
END IF;
-- Now, return the computed next business date to the calling program
RETURN v_NextDate;
END NextBusinessDate1;
29
TRIM and TO_CHAR functions
 TRIM(string)
Removes leading and trailing blanks
 TO_CHAR(date, ‘format’)
See Table 5-4 for a list of valid formats
The date field in the reservation table has been populated, but
the weekday field is NULL.
Write a query to populate the weekday field with the name of
the day that corresponds to the date specified in the date field.
UPDATE reservation SET weekday = TRIM(TO_CHAR(date,
‘DAY’));
NOTE: The ‘DAY’ format returns the name of the day with blanks
padded on the right such that the length is 9 characters.
30
Parameters
 May be passed by value or by reference

IN  by default, passed by reference

OUT  by default, passed by value

IN OUT  by default, passed by value

Passing by reference results in faster performance
 NOCOPY

A compiler hint to pass OUT & IN OUT parameters by
reference

Cannot use NOCOPY with IN parameters

Ex:

(P_outParameter IN OUT NOCOPY VARCHAR2) IS
31
Parameters
 Formal parameters can have default values
 Formal parameters with default values must appear as the
last items in the parameter list
 When calling a stored procedure or function, the
actual arguments can be passed by positional or
named notation
32
Calling Stored Procedures &
Functions
 With Parameters

Stored Procedure from SQL> prompt

CALL X(v_Variable1, …., v_VariableN);
OR CALL X(p_Parameter1 => v_Variable1,…);

EXEC X(v_Variable1,….,v_VariableN);

Stored Procedure from within PL/SQL block

EXECUTE IMMEDIATE ‘CALL X(……..)’; OR
X(v_Variable1,….,v_VariableN);
Function

Used in an expression

SELECT ElapsedDays(’01-JAN-1999’) FROM dual;
 Without Parameters

If the stored procedure (or function) does not have parameters,
then do not use parentheses to define or call the stored
procedure (or function)

More Related Content

PPTX
Functions
PPTX
Procedure and Functions in pl/sql
PPTX
pl/sql Procedure
PPTX
9. DBMS Experiment Laboratory PresentationPPT
PPTX
Lecture 3.2_Subprogrammm - Function.pptx
PPT
05 Creating Stored Procedures
PPT
DAC training-batch -2020(PLSQL)
PPTX
Unit 3
Functions
Procedure and Functions in pl/sql
pl/sql Procedure
9. DBMS Experiment Laboratory PresentationPPT
Lecture 3.2_Subprogrammm - Function.pptx
05 Creating Stored Procedures
DAC training-batch -2020(PLSQL)
Unit 3

Similar to Chapter 05 (Built-in SQL Functions) functions (20)

PPT
SQL / PL
PPT
Mis4200notes8 2
PPTX
Understanding pass by value and pass by reference is essential for effective ...
PPTX
Unit 4 plsql
PDF
Lecture Notes Unit5 chapter17 Stored procedures and functions
PPTX
Procedure n functions
PPTX
PLSQL.pptx
PPTX
Pl/SQL Procedure,Function & Packages - sub programs
PPT
plsql les01
PPT
SQL- Introduction to PL/SQL
PPS
Procedures/functions of rdbms
PDF
SQL Procedures & Functions
PPTX
Anchor data type,cursor data type,array data type
PPTX
Presentation1
PPTX
Unit 3(rdbms)
PPTX
Unit 3(rdbms)
PDF
PDF
PDF
Dbms 2011
SQL / PL
Mis4200notes8 2
Understanding pass by value and pass by reference is essential for effective ...
Unit 4 plsql
Lecture Notes Unit5 chapter17 Stored procedures and functions
Procedure n functions
PLSQL.pptx
Pl/SQL Procedure,Function & Packages - sub programs
plsql les01
SQL- Introduction to PL/SQL
Procedures/functions of rdbms
SQL Procedures & Functions
Anchor data type,cursor data type,array data type
Presentation1
Unit 3(rdbms)
Unit 3(rdbms)
Dbms 2011
Ad

Recently uploaded (20)

PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Cell Structure & Organelles in detailed.
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
human mycosis Human fungal infections are called human mycosis..pptx
O5-L3 Freight Transport Ops (International) V1.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Final Presentation General Medicine 03-08-2024.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
102 student loan defaulters named and shamed – Is someone you know on the list?
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Cell Structure & Organelles in detailed.
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Anesthesia in Laparoscopic Surgery in India
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Pharma ospi slides which help in ospi learning
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
STATICS OF THE RIGID BODIES Hibbelers.pdf
GDM (1) (1).pptx small presentation for students
2.FourierTransform-ShortQuestionswithAnswers.pdf
Final Presentation General Medicine 03-08-2024.pptx
Ad

Chapter 05 (Built-in SQL Functions) functions

  • 2. 2 Type of Functions  Character Functions  returning character values  returning numeric values  Numeric Functions  Date Functions  Conversion Functions  Group Functions  Error Reporting  Other Functions
  • 3. 3 Character Functions Returning Character Values  CHR  CONCAT  INITCAP
  • 4. 4 Character Functions Returning Character Values  LOWER  LPAD  LTRIM  NLS_INITCAP
  • 5. 5 Character Functions Returning Character Values  NLS_LOWER  NLS_UPPER  NLSSORT  REPLACE  RPAD
  • 6. 6 Character Functions Returning Character Values  RTRIM  SOUNDEX  SUBSTR  SUBSTRB  TRANSLATE  UPPER
  • 7. 7 Character Functions Returning Numeric Values  ASCII  INSTR  INSTRB  LENGTH  LENGTHB
  • 8. 8 Numeric Functions  ABS  ACOS  ASIN  ATAN  ATAN2
  • 9. 9 Numeric Functions  CEIL  COS  COSH  EXP  FLOOR  LN
  • 10. 10 Numeric Functions  LOG  MOD  POWER  ROUND  SIGN  SIN
  • 11. 11 Numeric Functions  SINH  SQRT  TAN  TANH  TRUNC
  • 12. 12 Date Functions  ADD_MONTHS  LAST_DAY  MONTHS_BETWEEN  NEW_TIME  NEXT_DAY  ROUND  SYSDATE  TRUNC
  • 13. 13 Conversion Functions  CHARTOROWID  CONVERT  HEXTORAW  RAWTOHEX  ROWIDTOCHAR
  • 14. 14 Conversion Functions  TO_CHAR  TO_DATE  TO_LABEL  TO_MULTI_BYTE  TO_NUMBER  TO_SINGLE_BYTE
  • 15. 15 Group Functions  AVG  COUNT  GLB  LUB
  • 16. 16 Group Functions  MAX  MIN  STDDEV  SUM  VARIANCE
  • 17. 17 Error Reporting Functions  SQLCODE  SQLERRM
  • 18. 18 Other Functions  BFILENAME  DECODE  DUMP  GREATEST  GREATEST_LB  LEAST
  • 19. 19 Other Functions  LEAST_LB  NVL  UID  USER  USERENV  VSIZE
  • 20. 20 Agenda  Stored Procedures  Functions  Parameters  Calling Stored Procedures & Functions  Examples
  • 21. 21 Stored Procedures  Named PL/SQL blocks that  Are stored in the database  May have formal parameters  Can return more than one value to the calling program  Can be called from  within other PL/SQL blocks as a PL/SQL statement by itself  SQL> prompt
  • 22. 22 PL/SQL Block vs. Stored Procedures DECLARE -- variable declaration BEGIN -- required executable EXCEPTION -- exception handling END; / CREATE OR REPLACE PROCEDURE X [(formal_parameters)] AS[IS] -- variable declaration BEGIN -- required executable EXCEPTION -- exception handling END X; / Anonymous PL/SQL Block Stored Procedure
  • 23. 23 Parameters  Parameters are optional  MUST be given a data type, but must NOT be given a size  Parameters have 3 modes  IN  Read-only within procedure/function  Default mode (if mode is not explicitly specified)  OUT  Has an initial value of NULL within the procedure/function  Ignores any values that the actual parameters have when the procedure/function is called  Can read from and write to  IN OUT  Value of actual parameters are passed into procedure/function  Can read from and write to
  • 24. 24 Stored Procedure with Parameters CREATE OR REPLACE PROCEDURE X ( p_Parameter1 IN VARCHAR2, p_Parameter2 IN NUMBER, p_Parameter3 OUT VARCHAR2, p_Parameter4 OUT NOCOPY NUMBER, p_Parameter5 IN OUT NUMBER DEFAULT 1) AS -- variable declaration BEGIN -- required executable EXCEPTION -- exception handling END X; /
  • 25. 25 set serveroutput on CREATE OR REPLACE PROCEDURE BoatReservations(p_Color IN VARCHAR2) AS CURSOR c_Reservations IS SELECT s.sname, r.day, r.bid FROM Sailor s, Reserve r, Boat b WHERE r.sid = s.sid AND r.bid = b.bid AND b.color = p_Color; v_Reservation c_Reservations%ROWTYPE; BEGIN OPEN c_Reservations; FETCH c_Reservations INTO v_Reservation; WHILE c_Reservations%FOUND LOOP DBMS_OUTPUT.PUT_LINE(v_Reservation.sname||' '||v_Reservation.day|| '||v_Reservation.bid); FETCH c_Reservations INTO v_Reservation; END LOOP; CLOSE c_Reservations; END BoatReservations; /
  • 26. 26 Functions  Named PL/SQL blocks that  Are stored in the database  May have formal parameters  MUST use the keyword RETURN to return only one value  RETURN passes control back to the calling program  Required for functions  Can be called from  within other PL/SQL blocks as part of an expression  SQL> prompt
  • 27. 27 Stored Procedures vs. Functions CREATE OR REPLACE PROCEDURE X [(parameters)] AS -- variable declaration BEGIN -- required executable EXCEPTION -- exception handling END X; / CREATE OR REPLACE FUNCTION X [(formal_parameters)] RETURN return_type IS[AS] -- variable declaration BEGIN -- required executable -- required RETURN statement RETURN Z; EXCEPTION -- exception handling END X; / Stored Procedure Function
  • 28. 28 CREATE OR REPLACE FUNCTION NextBusinessDate1 (p_Date DATE) RETURN DATE IS -- Variable that will contain the day that corresponds to the date parameter v_CurrentDay VARCHAR2(9); -- Variable that will contain the computed date of the next business day v_NextDate DATE; BEGIN /*First, determine the corresponding name of the day for the date parameter. It will be used later to determine the number of days by which the date should be incremented.*/ v_CurrentDay := UPPER(TRIM(TO_CHAR(p_Date, 'DAY'))); /*Based upon the name of the day and the business rule, calculate the next business date*/ IF v_CurrentDay = 'FRIDAY' THEN v_NextDate := p_Date + 3; ELSIF v_CurrentDay = 'SATURDAY' THEN v_NextDate := p_Date + 2; ELSE v_NextDate := p_Date + 1; END IF; -- Now, return the computed next business date to the calling program RETURN v_NextDate; END NextBusinessDate1;
  • 29. 29 TRIM and TO_CHAR functions  TRIM(string) Removes leading and trailing blanks  TO_CHAR(date, ‘format’) See Table 5-4 for a list of valid formats The date field in the reservation table has been populated, but the weekday field is NULL. Write a query to populate the weekday field with the name of the day that corresponds to the date specified in the date field. UPDATE reservation SET weekday = TRIM(TO_CHAR(date, ‘DAY’)); NOTE: The ‘DAY’ format returns the name of the day with blanks padded on the right such that the length is 9 characters.
  • 30. 30 Parameters  May be passed by value or by reference  IN  by default, passed by reference  OUT  by default, passed by value  IN OUT  by default, passed by value  Passing by reference results in faster performance  NOCOPY  A compiler hint to pass OUT & IN OUT parameters by reference  Cannot use NOCOPY with IN parameters  Ex:  (P_outParameter IN OUT NOCOPY VARCHAR2) IS
  • 31. 31 Parameters  Formal parameters can have default values  Formal parameters with default values must appear as the last items in the parameter list  When calling a stored procedure or function, the actual arguments can be passed by positional or named notation
  • 32. 32 Calling Stored Procedures & Functions  With Parameters  Stored Procedure from SQL> prompt  CALL X(v_Variable1, …., v_VariableN); OR CALL X(p_Parameter1 => v_Variable1,…);  EXEC X(v_Variable1,….,v_VariableN);  Stored Procedure from within PL/SQL block  EXECUTE IMMEDIATE ‘CALL X(……..)’; OR X(v_Variable1,….,v_VariableN); Function  Used in an expression  SELECT ElapsedDays(’01-JAN-1999’) FROM dual;  Without Parameters  If the stored procedure (or function) does not have parameters, then do not use parentheses to define or call the stored procedure (or function)