Chapter Nine Data Manipulation Language (DML) Functions

Slides:



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

Objectives After completing this lesson, you should be able to do the following: Describe various types of conversion functions that are available in.
Copyright © 2007, Oracle. All rights reserved Using Single-Row Functions to Customize Output Modified: October 21, 2014.
Chapter Six Data Manipulation Language (DML) Functions Objectives Single Row functions Character functions Number functions Date functions.
Advanced Topics: Business Intelligence Features
Introduction to Oracle9i: SQL1 Selected Single-Row Functions.
Using Single-Row Functions to Customize Output
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.
3 Single-Row Functions. 3-2 Objectives At the end of this lesson, you should be able to: Describe various types of functions available in SQL Use character,
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.
2-1 Copyright © Oracle Corporation, All rights reserved. Character Strings and Dates Character strings and date values are enclosed in single quotation.
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.
Database Programming Sections 1 & 2 – Case and Character Manipulations, number functions, date functions, conversion functions, general functions, conditional.
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.
Copyright  Oracle Corporation, All rights reserved. 3 Single-Row Functions.
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.
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.
6 Copyright © 2006, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
2-1 Copyright © Oracle Corporation, All rights reserved. Using the NULL Conditions Test for nulls with the IS NULL operator. SELECT last_name, manager_id.
EXPRESSION Transformation. Introduction ►Transformations help to transform the source data according to the requirements of target system and it ensures.
Conversion Functions.
Single Row Functions. Objectives –Use character, number, and date functions –Use conversion functions –Describe types of single row functions in SQL.
Single-Row Functions. SQL Functions FunctionInput arg 1 arg 2 arg n Function performs action OutputResultvalue.
Retrieving Data Using the SQL SELECT Statement. Objectives After completing this lesson, you should be able to do the following: – List the capabilities.
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.
Copyright س Oracle Corporation, All rights reserved. 3 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.
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
SQL Functions. SQL functions are built into Oracle Database and are available for use in various appropriate SQL statements. These functions are use full.
Built-in SQL Functions. 2 Type of Functions Character Functions returning character values returning numeric values Numeric Functions Date Functions Conversion.
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.
3 Copyright © 2007, Oracle. All rights reserved. Substitution Variables ra Oly l&On nase lce Int erU.
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.
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.
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.
4 Copyright © 2009, Oracle. All rights reserved. Using Conversion Functions and Conditional Expressions.
Enhanced Guide to Oracle 10g
Single-Row Functions Schedule: Timing Topic 55 minutes Lecture
Using Single-Row Functions to Customize Output
Ch. 3 Single-Row Functions
Single-Row Functions Schedule: Timing Topic 55 minutes Lecture
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
Single-Row Functions Schedule: Timing Topic 55 minutes Lecture
CS 3630 Database Design and Implementation
Using Single-Row Functions to Customize Output
SQL 101 3rd Session.
Built-in SQL Functions
Computations Done on table data
Using Single-Row Functions to Customize Output
المحاضرة 4 دوال الصف الواحد.
(SQL) Single-Row Functions
SQL 101 2nd Session.
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,
Contents Preface I Introduction Lesson Objectives I-2
Lecture 5 SQL FUNCTIONS.
Temporal Data Part V.
Presentation transcript:

Chapter Nine Data Manipulation Language (DML) Functions Objectives Single Row functions Character functions Number functions Date functions COSC 640 Chapter 9

Functions Introduction Types of functions Single row Multiple rows

Single Row Functions: FACTS: Act on each row Return one result per row May modify the data type Can be nested

Single Row Functions Character Number Date Conversion General

Character Manipulation: LOWER(Col | Exp) LOWER(‘Database course’) UPPER (Col | Exp) UPPER (‘Database course’) INITCAP (Col | Exp) INITCAP (‘Database course’)

Character Manipulation CONCAT (Col1 | Exp1, Col2 | Exp2) CONCAT(‘This ‘,’that’) SUBSTR(Col | Exp,n[,m]) SUBSTR(‘This is it’,2,5) LPAD(Col | Exp,n,’string’) LPAD(name,9,’.’)

Character Manipulation LENGTH(Col | Exp) LENGTH(‘this is it’) CHR(integer) CHR(97) INSTR(‘Computer’,’m’)

List(Characters): Example: SELECT LOWER(name), UPPER(major), LENGTH(name) FROM student; SELECT CONCAT(Name , Address) , GPA FROM Student;   SELECT Name || ‘--->‘ || Address , GPA

List(Characters): SELECT RPAD (Name, 40, ‘.’), GPA FROM Student; JOHN............... 3.1 MARY ............. 3.2 SELECT RPAD (Name, 20), GPA MARY 3.2 MOHAMMAD 3.3 SELECT LPAD (Name, 20), GPA MARY 3.2 MOHAMMAD 3.3

List(Characters): RTRIM(Col) RTRIM (Name) RTRIM (Name, ‘.’) LTRIM (Name, ‘ABC’)   LTRIM ( RTRIM( Name, ‘.’ ), ‘A’) From Student;

List(Characters): SELECT Name FROM Student WHERE LENGTH(Address)<20; SELECT Name, SUBSTR (SSN, 5 ,2) FROM Student; SELECT Name, SUBSTR (SSN,5)

List(Characters): SELECT RPAD (INITCAP(LOWER(Name)),70,’.’), SUBSTR (SSN,5) FROM Student; SELECT Name FROM Student WHERE SUBSTR (SSN,5,2)=’80’; SELECT Name, SUBSTR (SSN,-4) FROM Student;

List(Characters): ------------------------------------------------ SELECT Name, INSTR (Name,’R’) FROM Student;   ------------------------------------------------ MARY 3 JOHN 0 ROBIN 1 SELECT Name, INSTR (Name,’R’,1,2) SELECT Name, INSTR(Address,’FROSTBURG’) FROM Student;

List(Characters): Character Manipulations REPLACE(string, searchSt [,replace]) REPLACE(address,’21532’, ‘21211’) TRANSLATE (string, fromSt, toSt) TRANSLATE(‘12345678’, ‘123’, ‘999’) ASCII(string) ASCII(‘A’)

List(Characters): Character Manipulations SOUNDEX SELECT name, GPA FROM student WHERE SOUNDEX (name)=SOUNDEX(‘LAVALE’);

Character Manipulation New Functions: REGEXP_SUBSTR REGEXP_INSTR REGEXP_LIKE REGEXP_REPLACE

LIST(Numbers) ROUND (value, precision) TRUNC(value, precision) POWER(value,exponent) POWER(3,2)  MOD(value1, value2) MOD(900,400)

LIST(Numbers) SELECT ROUND(Salary,1) FROM Faculty; SELECT TRUNC(234.111,2), FROM DUAL; TRUNC(234.567); TRUNC(234.5678,-2);

DATE: Date is stored in an internal numeric format: century, year, month, day, hours, minutes, second Default date is ‘DD-MON-YY’ SYSDATE CURRENT_DATE STSTIMESTAMP 25-OCT-04 04.15.31.652000 PM

DATE: Example: List the ages of students SELECT name, SYSDATE - B_Date   SELECT name, SYSDATE - B_Date FROM student;

Date Date + number Date – number Date – date Date + number/24

DATE: MONTHS_BETWEEN(day1,day2) SELECT name, MONTHS_BETWEEN(SYSDATE , B_Date) age_in_month FROM Student;

DATE: ADD_MONTHS (date,n) SELECT name, ADD_MONTHS(B_Date,5) age FROM Student; SELECT name, ADD_MONTHS(B_Date,-15) age

DATE: ROUND(date [,fmt]) SELECT name, ROUND (B_Date,’MONTH’)   SELECT name, ROUND (B_Date,’MONTH’) FROM Student; SELECT name, ROUND(B_Date,’YEAR’)

DATE: ROUND(date [,fmt]) SELECT ’25-OCT-04’ - ROUND (SYSDATE)   SELECT ’25-OCT-04’ - ROUND (SYSDATE) FROM DOAL; SELECT name, ROUND(B_Date,’YEAR’) FROM Student;

DATE: NEXT_DAY SELECT cycledate FROM paydate; SELECT NEXT_DAY(cycledate, ‘FRIDAY’)

DATE: LAST_DAY (date) SELECT LAST_DAY (SYSDATE) “Last”, SYSDATE, LAST_DAY (SYSDATE) – SYSDATE “Left” FROM DUAL; Last SYSDATE Left -------------------------------------------------------- 31-MAY-05 28-MAY-05 4

Conversion Function: Implicit conversion (Automatic): CHAR or VARCHAR2 to NUMBER CHAR or VARCHAR2 to DATE NUMBER to VARCHAR2 DATE to VARCHAR2

Conversion Function: Explicit datatype conversion: TO_CHAR (NUMBER [,‘fmt’] ) TO_CHAR (DATE [,‘fmt’] ) TO_DATE (CHAR [,‘fmt’] ) TO_NUMBER (CHAR [,‘fmt’] )

Conversion Function: SELECT TO_CHAR(b_date,’MM/YY’) FROM student; Format: YYYY YYY YY RR YEAR MM MONTH DD DY DAY HH HH12 HH24 MI SS

Conversion Function: EXTRACT (YEAR FROM B_Date) MONTH DAY HOUR MINUTE SECOND SELECT EXTRACT (YEAR FROM DATE ‘1999-01-08’) FROM DUAL; --1999 SELECT EXTRACT (YEAR FROM TO_DATE (B_Date, ‘DD-MON-YY’) FROM Student;

Conversion Function: SELECT SUBSTR(TO_CHAR(111223333),1,3) ||‘-’ || FROM Student;

Conversion Function: SELECT SUBSTR(ssn,1,3) || ‘-’ || SUBSTR(ssn,4,2) FROM Student;

CAST CAST (expression) AS (Type); CAST (B_Date AS CHAR); CHAR NUMBER DATE ROW ROWID VARCHAR2 TIMESTAMP 1 X X X X X 2 X X 3 X X 4 X X 5 X X CAST (B_Date AS CHAR);

NULLIF NULLIF (Exp1, Exp2) IF Exp1 = Exp2 THEN RETURN NULL ELSE RETURN SELECT Name, NULLIF (a.major, b.major) “oldMajor” FROM Student a, StudentHistory b WHERE a.ID = b.ID;

NVL NVL (Exp1,Exp2) SELECT Name, NVL FROM Sales; (TO_CHAR (Commission_pct, ‘Not Applicable’)) commission FROM Sales;

NVL2 NVL2 (Exp1, Exp2, Exp3) SELECT Name, NVL2 (Commission_pct, Salary + Salary * Commission_pct, Salary) FROM Employee;

COALESCE COALESCE (exp_List) Return the first non_null expression in the expression list: COALESCE (Email, Address, Phone_#, Phone_W) SELECT Product_ID, Product_Name, List_Price, Min_Price, COALESCE (List_Price, Min_Price, 10) ”Sale Price” FROM Product;

Use of DECODE: DECODE: DECODE (col/exp, compare1, result1 [,default] ) SELECT name, salary, DECODE (Dept, ‘COSC’, salary*2.2, ‘MATH’, salary*1.2, ‘ART’, salary*0.2, salary) FROM Faculty; COSC 640 Chapter 9