W6 - Practical 5
W6 - Practical 5
Practical 5
Lesson Objectives
o Describe various types of functions that are available
in SQL
o Use single row functions in SELECT statements
o Describe the use of conversion functions
Using Scripts
o Run the following scripts before the practical.
o Northwoods.sql
o HR.sql
3
Dummy Table
o The DUAL is special one row, one column
dummy table present by default in all Oracle
databases.
o The owner of DUAL is SYS (SYS owns the
data dictionary, therefore DUAL is part of the
data dictionary.) but DUAL can be accessed
by every user.
DESCRIBE DUAL;
Dummy Table
Try the following SQL statements:
SELECT 'ABCDEF12345'
FROM DUAL;
Result: ABCDEF12345
SELECT 15+10-5*5/5
FROM DUAL;
Result: 20
SQL Functions
o Two Types:
n Single-Row Function
n Multiple-Row Function
o Single-Row Function
n Operate on single rows only and return one result per row.
n character, number, date, conversion, general
o Multiple-Row Function
n Can manipulate groups of rows to give one result per
group of rows.
n Also known as Group Function.
Single-Row Example
S_ID S_LAST S_FIRST S_MI S_ADDRESS S_CITY S_STATE S_ZIP
Single Row:
- Round - Lower - Concat - Instr
- Trunc - Upper - Substr - Replace
- Sign - Initcap - Length - Trim
Multiple-Row Example
COURSE_ID CALL_ID COURSE_NAME CREDITS
o TRUNC
n Truncates a value to a specified position.
o SIGN
n SIGN function returns a value indicating the sign of a
number.
9
Single-Row Number Functions
SELECT ROUND(45.927,2),
ROUND (45.927,0),
ROUND (45.927),
ROUND(45.927,-1)
FROM DUAL;
13
Single-Row Character Functions
o Case Manipulation
n LOWER, UPPER, INITCAP
o Character Manipulation
n CONCAT, SUBSTR, LENGTH, INSTR,
REPLACE, TRIM
14
Single-Row Character Functions
o LOWER() - Returns the string with all
characters converted to lowercase letters.
15
Single-Row Character Functions
o CONCAT() - Concatenates (joins) two
strings.
16
Single-Row Character Functions
o INSTR() - Searches a string for a substring and
returns an integer indicating the position of the
character in string that is the first character of this
occurrence.
18
Using Case Manipulation Functions
SELECT term_id, term_desc, status
FROM term;
19
Using Case Manipulation Functions
SELECT s_last, s_first, s_dob
FROM student
WHERE s_last = 'mobley';
Result: no rows selected
SELECT CONCAT('ABC','DEF')
FROM DUAL;
22
Character Manipulation
SELECT SUBSTR('ABCDEF',2,3)
FROM DUAL;
SELECT INSTR('ABCDEFG','C')
FROM DUAL;
23
Character Manipulation
SELECT bldg_code, room
FROM location
WHERE bldg_code = 'CR';
24
Concatenation Operator
o Links columns or character strings to other
columns.
o Is represented by two vertical bars, ||
Bldg No
-----------------
CR-101
CR-103
CR-105
CR-202
BUS-105
BUS-211
BUS-402
BUS-404
BUS-421
BUS-424
BUS-433
o Try the exercise given.
Working with Dates
o The Oracle database stores dates in an internal
numeric format: century, year, month, day, hours,
minutes, and seconds.
o The default date format: DD-MON-RR.
n Enables you to store 21st-century dates in the 20th
century by specifying only the last two digits of the year
n Enables you to store 20th-century dates in the 21st
century in the same way
Copyright © 2014 – 2015 Iconix Consulting Sdn Bhd. All rights reserved. 28
SYSDATE Function
Copyright © 2014 – 2015 Iconix Consulting Sdn Bhd. All rights reserved. 29
Single-Row Date Functions
o Add_months(date,n)
o Months_between (date1, date2)
o next_day(date, 'Day')
o Last_day (date)
o ROUND(date[, 'fmt'])
o TRUNC(date[, 'fmt'])
Copyright © 2014 – 2015 Iconix Consulting Sdn Bhd. All rights reserved. 30
Single-Row Date Functions
SELECT ADD_MONTHS(sysdate, 2)
FROM dual;
SELECT MONTHS_BETWEEN(hire_date,
SYSDATE)
FROM employees
WHERE employee_id=114;
Copyright © 2014 – 2015 Iconix Consulting Sdn Bhd. All rights reserved. 31
Single-Row Date Functions
SELECT NEXT_DAY(sysdate, 'FRIDAY')
FROM dual;
SELECT LAST_DAY(sysdate)
FROM dual;
Copyright © 2014 – 2015 Iconix Consulting Sdn Bhd. All rights reserved. 32
Practice 5.2
Copyright © 2014 – 2015 Iconix Consulting Sdn Bhd. All rights reserved. 33
Single-Row Date Functions
SELECT ROUND(SYSDATE, 'MONTH') FROM DUAL;
SELECT ROUND(SYSDATE, 'YEAR') FROM DUAL;
ROUND(TO_
---------
01-JUL-08
ROUND(TO_
---------
01-AUG-08
34
Copyright © 2014 – 2015 Iconix Consulting Sdn Bhd. All rights reserved.
Single-Row Date Functions
Copyright © 2014 – 2015 Iconix Consulting Sdn Bhd. All rights reserved. 35
Nesting Functions
o Single-row functions can be nested to any level.
o Nested functions are evaluated from deepest level to
the least deep level.
36
Nesting Functions
SELECT LENGTH(SUBSTR(course_name, 9, 5))
FROM COURSE;
37
Conversion Functions
o SQL provides three functions to convert a value
from one data type to another.
n TO_CHAR
n TO_DATE
n TO_NUMBER
38
Numerical Format Models
39
Using the TO_CHAR Function with Numbers
o Translates a value of NUMBER data type to
VARCHAR2 data type.
o TO_CHAR(field_name, 'format_model')
40
Using the TO_CHAR Function with Dates
o translates a value of DATE data type to VARCHAR2
data type.
o TO_CHAR(date, 'format_model')
o The format model:
n Must be enclosed by single quotation marks
n Is case-sensitive
n Can include any valid date format element
n Is separated from the date value by a comma
41
Date Format Models
42
Date Format Models
43
Formatting Date/time
SELECT s_id, s_last, s_dob
FROM student;
44
Using the TO_DATE Functions
o Convert a character string to a date format using the
TO_DATE function:
n TO_DATE(char[, 'format_model'])
45
Using the TO_DATE Functions in Search Expressions
*
SELECT s_id, s_first, s_dob
ERROR at line 2:
FROM student ORA-01843: not a valid
WHERE s_dob = '07/14/1985'; month
47
Using the TO_DATE Functions in Search Expressions
Birthday
-------------------------------------------------------------
Miller Sarah's birthday is on 14, July, 1985.
Umato Brian's birthday is on 19, August, 1985.
Black Daniel's birthday is on 10, October, 1982.
Mobley Amanda's birthday is on 24, September, 1986.
Sanchez Ruben's birthday is on 20, November, 1986.
Connoly Michael's birthday is on 4, December, 1986.
Using the TO_NUMBER Functions
o Convert a character string to a number format using
the TO_NUMBER function:
n TO_NUMBER(char[, 'format_model'])
51
Using the TO_NUMBER Function
o SELECT TO_NUMBER('1210.73', '9999.99’)
FROM dual;
Result: 1210.73
o SELECT TO_NUMBER(room)
FROM location;
Do it yourself
1. Retrieve the student last name who were born in
‘September’.
2. Get all employees last name and hired date, who
were hired in ‘Jan’.
3. Get all employees last name and hired date, who
were hired, arrange the hired date in alphabetical
order in month regardless of year.
o Try the exercise given.