Built-in SQL Functions. 2 Type of Functions Character Functions returning character values returning numeric values Numeric Functions Date Functions Conversion.

Slides:



Advertisements
Similar presentations
Copyright  Oracle Corporation, All rights reserved. 2 Single-Row Functions.
Advertisements

Copyright © 2007, Oracle. All rights reserved Using Single-Row Functions to Customize Output Modified: October 21, 2014.
PL/SQL.
Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. SQL Workshop Day 2.
Lecture-5 Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language.
BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,
Databases Lab 5 Further Select Statements. Functions in SQL There are many types of functions provided. The ones that are used most are: –Date and Time.
Advanced Topics: Business Intelligence Features
Introduction to Oracle9i: SQL1 Selected Single-Row Functions.
Introduction to PL/SQL
FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language.
Using Single-Row Functions to Customize Output
Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.
ORACLE ONLINE TRAINING Contact our Support Team : SOFTNSOL India: Skype id : softnsoltrainings id:
Ch. 3 Single-Row Functions Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
Single-Row Functions. SQL Functions Functions are a very powerful feature of SQL and can be used to do the following: Perform calculations on data Modify.
3-1 Copyright  Oracle Corporation, All rights reserved. SQL Functions FunctionInput arg 1 arg 2 arg n Function performs action OutputResultvalue.
1 Introduction to PL/SQL. 2  Procedural programming language  Uses detailed instructions  Processes statements sequentially  Combines SQL commands.
Oracle FUNCTIONS. Comment ScreenShot (in 10g) General Example of null Foreign Key: create table deptcs( deptno NUMBER(4) primary key, hiredate DATE,
3 Copyright © Oracle Corporation, All rights reserved. Single-Row Functions.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL.
Chapter 5 Selected Single-Row Functions. Chapter Objectives  Use the UPPER, LOWER, and INITCAP functions to change the case of field values and character.
Functions Oracle Labs 5 & 6. 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 2 SQL Functions Function arg n arg 2 arg 1. Input Resulting Value.
Oracle Database Administration Lecture 3  Transactions  SQL Language: Additional information  SQL Language: Analytic Functions.
PL/SQLPL/SQL Oracle11g : PL/SQL Programming Chapter 6 Functions.
3 Copyright © 2004, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
Single – Row Functions. Objectives After completing this lesson, you should be able to do the following:  Describe various types of functions available.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
1. 1. Which type of argument passes a value from a procedure to the calling program? A. VARCHAR2 B. BOOLEAN C. OUT D. IN 2.
Single Row Functions Week 2. Objectives –Describe types of single row functions in SQL –Describe and use character, number, date, general and conversion.
Oracle 11g: SQL Chapter 10 Selected Single-Row Functions.
LECTURE 1 INTRODUCTION TO PL/SQL Tasneem Ghnaimat.
SQL Oracle PL/SQL. Select SELECT column1, column2,...columnN FROM table_name WHERE condition; SELECT column1, column2,...columnN FROM table_name WHERE.
Chapter 3 Selected Single-Row Functions and Advanced DML & DDL.
PL/SQL Oracle's Database Programming Language. Remember: Set serveroutput on With serveroutput off (default) executing procedure: With serveroutput on:
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
6 Copyright © 2006, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
EXPRESSION Transformation. Introduction ►Transformations help to transform the source data according to the requirements of target system and it ensures.
Single Row Functions. Objectives –Use character, number, and date functions –Use conversion functions –Describe types of single row functions in SQL.
Copyright  Oracle Corporation, All rights reserved. 3 Single-Row Functions.
3 Copyright © 2009, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
3 Copyright © Oracle Corporation, All rights reserved. Single-Row Functions.
3 Copyright © Oracle Corporation, All rights reserved. Single-Row Functions.
3 第三讲 Single-Row Functions. Objectives After completing this lesson, you should be able to do the following: Describe various types of functions available.
SQL Functions. SQL functions are built into Oracle Database and are available for use in various appropriate SQL statements. These functions are use full.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
Using Single-Row Functions to Customize Output
Oracle & SQL. Oracle Data Types Character Data Types: Char(2) Varchar (20) Clob: large character string as long as 4GB Bolb and bfile: large amount of.
4/2/16. Ltrim() is used to remove leading occurrences of characters. If we don’t specify a character, Oracle will remove leading spaces. For example Running.
Gollis University Faculty of Computer Engineering Chapter Five: Retrieval, Functions Instructor: Mukhtar M Ali “Hakaale” BCS.
Introduction to PL/SQL N. Dimililer. About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and.
3 Copyright © 2004, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
3 Copyright © 2009, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
 Reviewing basic architecture concepts  Oracle 10g Architecture  Main features of 9i and 10g
Copyright  Oracle Corporation, All rights reserved. 3 Single-Row Functions.
Single Row Functions Part I Week 2. Objectives –Describe types of single row functions in SQL –Describe and use character, number and date SQL functions.
Oracle SQL.
Pl/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-3
Open Source Server Side Scripting MySQL Functions
What’s So Great About PL/SQL
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
Agenda Summary of last class Cursors Loops Records Simple Loops
SQL 101 3rd Session.
Built-in SQL Functions
ISYS Built-in Functions
Computations Done on table data
Index Note: A bolded number or letter refers to an entire lesson or appendix. A Adding Data Through a View ADD_MONTHS Function 03-22, 03-23,
Trainer: Bach Ngoc Toan– TEDU Website:
Presentation transcript:

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 BlockStored 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_Reservationc_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_CurrentDayVARCHAR2(9); -- Variable that will contain the computed date of the next business day v_NextDateDATE; 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)