Chapter Seven (part one) Data Manipulation Language (DML) Functions
-
Upload
william-kennedy -
Category
Documents
-
view
43 -
download
0
description
Transcript of Chapter Seven (part one) Data Manipulation Language (DML) Functions
![Page 1: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/1.jpg)
Chapter Seven (part one)
Data Manipulation Language (DML)Functions
Objectives Single Row functions Character functions Number functions Date functions
![Page 2: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/2.jpg)
2
Functions Introduction Types of functions
Single row Multiple rows
![Page 3: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/3.jpg)
Single Row Functions:
FACTS: Act on each row Return one result per row May modify the data type returned type Can be nested
![Page 4: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/4.jpg)
4
Single Row Functions Character Number Date Conversion General
![Page 5: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/5.jpg)
5
Character Manipulation: LOWER(Col | Exp)
LOWER(‘Database course’)
UPPER (Col | Exp)UPPER (‘Database course’)
INITCAP (Col | Exp)INITCAP (‘Database course’)
![Page 6: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/6.jpg)
6
Practice:
Display last name and first name of customers. Last name in upper case. First character of each first name in upper case, the rest in lower case.
Example:
SMITH Lori
![Page 7: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/7.jpg)
7
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,’.’)
![Page 8: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/8.jpg)
8
Character Manipulation LENGTH(Col | Exp)
LENGTH(‘this is it’)
CHR(integer)CHR(97)
INSTR(‘Computer’,’m’)
![Page 9: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/9.jpg)
9
List(Characters): Example:
SELECT LOWER(name), UPPER(major), LENGTH(name)
FROM student;
SELECT CONCAT(Name , Address) , GPAFROM Student;
SELECT Name || ‘--->‘ || Address , GPA
FROM Student;
![Page 10: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/10.jpg)
10
List(Characters): SELECT RPAD (Name, 40, ‘.’), GPA
FROM Student;JOHN............... 3.1MARY ............. 3.2
SELECT RPAD (Name, 20), GPAFROM Student;
MARY 3.2 MOHAMMAD 3.3
SELECT LPAD (Name, 20), GPAFROM Student;
MARY 3.2 MOHAMMAD 3.3
![Page 11: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/11.jpg)
11
Practice:
Display the last name and address of each customer together. In the next column display customer phone number.
SMITH 11 MAIN ST FROSTBURG MD……..301 689 1111
![Page 12: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/12.jpg)
12
List(Characters): RTRIM(Col)
RTRIM (Name)RTRIM (Name, ‘.’)LTRIM (Name, ‘ABC’)
LTRIM ( RTRIM( Name, ‘.’ ), ‘a’)From Student;
![Page 13: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/13.jpg)
13
Practice:
Customer phone number is stored as: -301-689-1111:
Part 1- We would like to delete the first dash and the last colon from the phone numbers:
-301-689-1111:Part 2- Remove all dashes and colon
![Page 14: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/14.jpg)
14
List(Characters): SELECT Name
FROM StudentWHERE LENGTH(Address)<20;
SELECT Name, SUBSTR (SSN, 5 ,2)
FROM Student;
SELECT Name, SUBSTR (SSN,5)FROM Student;
![Page 15: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/15.jpg)
15
List(Characters): SELECT RPAD
(INITCAP(LOWER(Name)),70,’.’), SUBSTR (SSN,5)
FROM Student;
SELECT NameFROM StudentWHERE SUBSTR (SSN,5,2)=’80’;
SELECT Name, SUBSTR (SSN,-4)FROM Student;
![Page 16: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/16.jpg)
16
Practice:
Display the 10 characters from OrderPart description starting at location 6.
Example:Door Replacement Handle
![Page 17: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/17.jpg)
17
List(Characters): SELECT Name, INSTR (Name,’r’)
FROM Student; ------------------------------------------------
MARY 3JOHN 0ROBIN 1
SELECT Name, INSTR (Name,’r’,1,2)FROM Student;
SELECT Name, INSTR(Address,’Frostburg’)FROM Student;
![Page 18: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/18.jpg)
18
Practice:
We would like to switch the position of last name with the first name. Assume the attribute name consists of both first and last names with a blank character in between.
![Page 19: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/19.jpg)
19
List(Characters):
Character Manipulations
REPLACE(string, searchSt [,replace])REPLACE(address,’21532’, ‘21211’)
TRANSLATE (string, fromSt, toSt)TRANSLATE(‘12345678’, ‘123’, ‘999’)
ASCII(string)ASCII(‘A’)
![Page 20: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/20.jpg)
20
LIST(Numbers) ROUND (value, precision)
ROUND(234.1161,2)
TRUNC(value, precision)TRUNC(234.1161,2)
POWER(value,exponent)POWER(3,2)
MOD(value1, value2)MOD(900,400)
![Page 21: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/21.jpg)
21
LIST(Numbers) SELECT ROUND(Salary,1)
FROM Faculty;
SELECT TRUNC(234.111,2), FROM DUAL;
TRUNC(234.567);TRUNC(234.5678,-2);
![Page 22: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/22.jpg)
22
DATE: Date is stored in an internal numeric
format: century, year, month, day, hours, minutes, second
Default date is DD-MON-YY
SYSDATE
![Page 23: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/23.jpg)
23
DATE: Example:
List the ages of students SELECT name, SYSDATE -
B_DateFROM student;
![Page 24: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/24.jpg)
24
Practice:
Display today’s date.
![Page 25: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/25.jpg)
25
Date Date + number Date – number Date – date Date + number/24
![Page 26: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/26.jpg)
26
DATE: MONTHS_BETWEEN(day1,day2)
SELECT name, MONTHS_BETWEEN(SYSDATE , B_Date)
age_in_month
FROM Student;
![Page 27: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/27.jpg)
27
DATE: ADD_MONTHS (date,n) SELECT name,
ADD_MONTHS(B_Date,5) age
FROM Student;
![Page 28: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/28.jpg)
28
DATE: ROUND(date [,fmt]) SELECT name, ROUND
(B_Date,’MONTH’)FROM Student;
SELECT name, ROUND(B_Date,’YEAR’)FROM Student;
![Page 29: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/29.jpg)
29
Conversion Function:
Implicit conversion (Automatic):
CHAR or VARCHAR2 to NUMBER CHAR or VARCHAR2 to DATE NUMBER to VARCHAR2 DATE to VARCHAR2
![Page 30: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/30.jpg)
30
Conversion Function:
Explicit datatype conversion:
TO_CHAR (NUMBER [,‘fmt’] ) TO_CHAR (DATE [,‘fmt’] ) TO_DATE (CHAR [,‘fmt’] ) TO_NUMBER (CHAR [,‘fmt’] )
![Page 31: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/31.jpg)
31
Conversion Function:
SELECT TO_CHAR(b_date,’MM/YY’)FROM student;
Format: YYYY YEAR MM MONTH DY DAY
![Page 32: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/32.jpg)
32
Conversion Function:
SELECTSUBSTR(TO_CHAR(111223333),1,3)
||‘-’ || SUBSTR
(TO_CHAR(111223333),4,2) || ‘-’ ||SUBSTR(TO_CHAR(111223333),6)
FROM Student;
![Page 33: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/33.jpg)
33
Conversion Function:
SELECT SUBSTR(ssn,1,3) || ‘-’ || SUBSTR(ssn,4,2) || ‘-’ ||SUBSTR(ssn,6)
FROM Student;
![Page 34: Chapter Seven (part one) Data Manipulation Language (DML) Functions](https://reader035.fdocuments.us/reader035/viewer/2022062422/56812e31550346895d93964e/html5/thumbnails/34.jpg)
34
Use of DECODE: DECODE:
DECODE (col/exp, compare1, result1 [,compare2, result2, …] [,default] )
SELECT name, salary, DECODE (Dept, ‘COSC’, salary*2.2,
‘MATH’, salary*1.2, ‘ART’, salary*0.2, ‘salary)
FROM Faculty;