Академический Документы
Профессиональный Документы
Культура Документы
Section 2
Instructor Guide
Table of Contents
Lesson Preparation
When there is extra time in class, have students work on the self-test software.
What to Watch For
This is a lesson that requires considerable student practice. Check to make sure that, when
students get output returned from a query, it is the correct output.
Relate the idea of presenting information in a more-readable format to the process of preparing
the data-modeling presentation for the clients. It was important to have the information in a
format that was easily understood by everyone.
Connections
Ask students to find examples of formatted data on the Internet or in the newspaper. Sources
include the financial page, statistical information, grade reports, etc. Ask them to write a SQL
statement to convert default format to the source format.
Tell Me / Show Me
Tell Me / Show Me
Begin this lesson with a review question:
How can we find out what the date will be exactly five months from today?
Answer:
SELECT ADD_MONTHS(SYSDATE, 5)
FROM DUAL;
Before beginning this lesson's discussion of data-type conversions and the Oracle Server's
capability to implicitly do data-type conversions, explain to students the difference between
"implicit" and "explicit." Use the example, "When she rolled her eyes and sighed, Sue
implicitly told Bill she didn't want to go the dance with him. Bill didn't understand her actions
until she explicitly said, 'Bill, I already told you, I have a date for the dance!'" The dictionary
defines "implicit" as something that is "implied but not directly expressed" and explicit as
"clearly formulated or defined."
Tell Me / Show Me
Tell Me / Show Me
Begin this lesson with a review question:
How can we find out what the date will be exactly five months from today?
Answer:
SELECT ADD_MONTHS(SYSDATE, 5)
FROM DUAL;
Before beginning this lesson's discussion of data-type conversions and the Oracle Server's
capability to implicitly do data-type conversions, explain to students the difference between
"implicit" and "explicit." Use the example, "When she rolled her eyes and sighed, Sue
implicitly told Bill she didn't want to go the dance with him. Bill didn't understand her actions
until she explicitly said, 'Bill, I already told you, I have a date for the dance!'" The dictionary
defines "implicit" as something that is "implied but not directly expressed" and explicit as
"clearly formulated or defined."
In this lesson, students will explicitly define data-type conversions to ensure the reliability of
SQL statements.
Tell Me / Show Me
Tell Me / Show Me
After introducing each of the four data-type conversions, explain the format model table and
the use of "fm" and "spth."
STOP and allow students time to practice.
Conversion answers:
1. DATE CONVERSION TO CHARACTER DATA
June 19th, 2004 TO_CHAR(hire_date, 'Month ddth, YYYY')
January 1, 2000 TO_CHAR(hire_date, 'fmMonth dd, YYYY')
MAR 5, 2001 TO_CHAR(hire_date, 'fmMON dd, YYYY')
returns:
100 MAY 14, 2004
105 APRIL 28, 2004
Tell Me / Show Me
Tell Me / Show Me
After introducing each of the four data-type conversions, explain the format model table and
the use of "fm" and "spth."
STOP and allow students time to practice.
Conversion answers:
1. DATE CONVERSION TO CHARACTER DATA
June 19th, 2004 TO_CHAR(hire_date, 'Month ddth, YYYY')
January 1, 2000 TO_CHAR(hire_date, 'fmMonth dd, YYYY')
MAR 5, 2001 TO_CHAR(hire_date, 'fmMON dd, YYYY')
Tell Me / Show Me
Tell Me / Show Me
After introducing each of the four data-type conversions, explain the format model table and
the use of "fm" and "th."
STOP and allow students time to practice.
Conversion answers:
(The date will vary depending on the SYSDATE.)
SELECT TO_CHAR(SYSDATE, 'fmMonth ddth, YYYY' )
FROM DUAL;
returns: August 6th, 2004
Tell Me / Show Me
Tell Me / Show Me
Conversion answers:
$3000.00 TO_CHAR(salary, '$9999.99')
4,500 TO_CHAR(salary, '9,999')
9,000.00 TO_CHAR(salary, '9,999.99')
0004422 TO_CHAR(salary, '0009999')
Tell Me / Show Me
Tell Me / Show Me
Ask students to use the DUAL table to convert their age to a number.
Tell Me / Show Me
Tell Me / Show Me
The fx format may require more practice. Remind students to think of fx as "format exact" so
the fxdate of the format model must exactly match the format of the date being converted.
Ask students to use the fx modifier to convert each of the following to the default date format.
June19 2004
SELECT TO_DATE('June19 2004', 'fxMonthDD RRRR')AS Convert
FROM DUAL;
July312004
SELECT TO_DATE('July312004', 'fxMonthDDRRRR')AS Convert
FROM DUAL;
Tell Me / Show Me
Tell Me / Show Me
The fx format may require more practice. Remind students to think of fx as "format exact" so
the fxdate of the format model must exactly match the format of the date being converted.
Ask students to use the fx modifier to convert each of the following to the default date format.
June19 2004
July312004
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
Use the following example to show the effects of the RRRR and YYYY formats:
You'll get about three rows. Examine the WHERE clause. It asks to return only those hire dates
less than (before) those of 01-Jan-90. The RR used in the format model here makes sure that
the date is seen as 01-Jan-90. The three rows returned were those few people who were hired
before 01-Jan -90.
Try It / Solve It
Try It / Solve It
1. SELECT last_name, TO_CHAR(birthdate, 'Month fmDD, RRRR')AS "Birthday"
FROM f_staffs;
3. SELECT 'The promotion began on the '|| TO_CHAR(start_date, 'ddspth "of" Month
YYYY')as "Date"
FROM f_promotional_menus
WHERE code = 110;
Try It / Solve It
Try It / Solve It
8. SELECT TO_CHAR(TO_DATE('25-DEC-04','dd-MON-yy'),'Month ddth,
YYYY')Convert,TO_CHAR(TO_DATE('25-DEC-04','dd-MON-yy'),'MONTH DDth,
YYYY')Convert,TO_CHAR(TO_DATE('25-DEC-04','dd-MON-yy'),'month ddth, YYYY')AS
Convert
FROM DUAL;
Lesson Preparation
None.
What to Watch For
Students may be suffering from "information overload" at this point. Keep encouraging
practice and use the self-test software as a break and reinforcement. Ask students to generate
query examples and challenge the class.
Connections
Many of the functions students learned in this lesson, they do every day.
Ask: Have you ever gone to a store to buy a something you were looking for only to find out
that the store didn't have it? Have you been in the lunch line only to find out that your favorite
item was sold out? Did you choose something else? If you've ever had this experience, you
actually were doing a NVL2 or COALESCE function. If the first choice was null, choose the
second or the third.
Tell Me / Show Me
Tell Me / Show Me
Begin the lesson with a review question: How would you display today’s date as: 15 January
2003?
Students should be familiar with nested functions taught in mathematics classes. In the
example, ask students to list the order of processing.
Step 1: The hire date is going to have six months added to it.
Step 2: The first Friday following the future day will be identified.
Step 3: The default date format will be formatted to read and display the Friday in a format
similar to: Friday, December 18TH, 1987, and will appear in the output under the column
name "Next Evaluation."
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
Tell Me / Show Me
Students have not formally studied data types. Tell them that LONG is variable-length
character data up to 2 gigabytes in size.
The NVL2 sample code illustrates a SQL statement returning an employee's salary and income.
For employees who work on commission, the income is equivalent to the salary plus the salary
multiplied by the commission percentage. For those who do not work on commission, the
income is equivalent to the salary.
Tell Me / Show Me
Try It / Solve It
Try It / Solve It
Try It / Solve It
Answers:
Encourage the students to use aliases to make the output more readable.
1. SELECT name, NVL2(end_date,'end in two weeks', SYSDATE)AS Promotion
FROM f_promotional_menus;
2. SELECT last_name,NVL(overtime_rate,0)AS "Overtime Status"
FROM f_staffs;
3. SELECT last_name, TO_CHAR(NVL(overtime_rate,5.00), '$9999.99')AS "Overtime Rate"
FROM f_staffs;
4. SELECT last_name, NVL(manager_id, 9999)
FROM f_staffs;
Try It / Solve It
Try It / Solve It
Answers:
5. c
6.
Extension:
Show/discuss Demo: The Basics of Using Functions
Lesson Preparation
None.
What to Watch For
The CASE and DECODE functions need practice. Remind students to think of these functions
as IF-THEN-ELSE decision operations. Read the syntax to them in this way.
Tell Me / Show Me
Tell Me / Show Me
Begin this lesson with a review question from previous material. Ask students to write a
SELECT statement to show "No Comment" if an event_id on the D_PLAY_LIST_ITEMS
table does not have a comment.
Answer:
SELECT NVL(comments, 'No Comment')
FROM D_PLAY_LIST_ITEMS;
Tell Me / Show Me
Tell Me / Show Me
Students will have difficulty distinguishing between CASE and DECODE. The CASE
expression is a more flexible version of the DECODE function. The CASE expression
complies with ANSI SQL; DECODE is specific to Oracle syntax.
Try It / Solve It
Try It / Solve It
Answers:
1. SELECT name,ROUND(MONTHS_BETWEEN(SYSDATE, end_date),0) AS "Past
Promos"
FROM f_promotional_menus;
Try It / Solve It
Try It / Solve It
Answers:
5. SELECT first_name, last_name, manager_id, commission_pct, COALESCE(manager_id,
commission_pct, 99999) AS "Review"
FROM employees
WHERE department_id IN( 80,90);
Lesson Preparation
The practice exercises in Lesson 4 and Lesson 5 prepare students for the Lesson 5 Quiz
measuring the objectives for SQL functions -- case, character, number, date, conversion,
general, and conditional expressions. Students should complete all practices. Review answers
with students.
What to Watch For
The practice exercises are more challenging. Check for understanding.
Connections
None.
Tell Me / Show Me
Tell Me / Show Me
Try It / Solve It
Students will find these practices more challenging. Assist them in dissecting the problem into
the information needed and the order of operations for accomplishing the task. Encourage
students to use aliases to make the output more readable.
Answers:
1. SELECT first_name, last_name,zip
FROM f_customers
WHERE LENGTH(zip) < 10;
2. a. LPAD (CDN)
b. ROUND (DN)
c. TRUNC (DN)
d. LENGTH (C)
e. LAST_DAY (D)
f. INSTR (CDN)
g. CONCAT (CDN)
Tell Me / Show Me
Try It / Solve It
Answers:
4. Jason and Jamie forgot that the data types of the NVL elements must be the same data type!
A TO_CHAR function is needed to convert the number overtime_rate to a character value.
SELECT first_name, last_name, NVL(TO_CHAR(overtime_rate, '9999999'),'no overtime')As
"Payrate"
FROM f_staffs;
Try It / Solve It
Try It / Solve It
Answers:
6. ___T__a. TO_CHAR is required to convert the date '03-JUN-04' to June 3, 2004
___F__b. TO_NUMBER will convert '23-NOV-02' to use with ADD_MONTHS
___F__c. TO_DATE will convert SYSDATE to today's date
___F__d. TO_NUMBER('101', '$99999') will convert 101 to a number
___T__e. TO_CHAR(salary, '$9999.99') will convert number to character format
___T__f. TO_NUM(varchar2 column) will convert character data to a number
___T__g. TO_CHAR(SYSDATE, 'Month fmdd, yyyy') will format the date
Try It / Solve It
Try It / Solve It
Answers:
8. SELECT SUBSTR(address, -5,5)AS "Old Zip", SUBSTR(address, -5,5)||'-2345' AS "New
Zip"
FROM d_venues
WHERE ID= 105;
9. SELECT ROUND(SYSDATE, 'YEAR') AS YEAR, ROUND(SYSDATE, 'MONTH')AS
MONTH, TRUNC(SYSDATE, 'YEAR')AS YEAR, TRUNC(SYSDATE, 'MONTH')AS
MONTH
FROM DUAL;
10. SELECT name, ROUND((SYSDATE - START_DATE),0) AS "Days"
FROM f_promotional_menus;
11. SELECT LOWER(CONCAT(LOWER(SUBSTR(job_title, 1, 5)),'* ' )) "Job Description"
FROM jobs;
12. The expression will be evaluated from the innermost expression outward.
Lesson Preparation
The practice exercises are challenging. Students should achieve 70% or better on the quiz.
Provide enough time to review for the quiz. After the quiz, review the most difficult questions.
Encourage students to practice their skills using self-test software.
Give a brief overview of what's next:
Section 3, Lesson 1, "Career Explorations" -- develop a thirteenth-year plan and look into what
you want your future to be.
Review Section 2 of the Study Guide and Vocabulary with students.
Review practice exercise answers with students. Focus on the method for solving the problem,
not just the answer.
What to Watch For
Students are asked to apply concepts learned in the practice exercises. The difficulty of the
problems may be challenging for some students.
Provide enough time to review for the quiz. After the quiz, review the most difficult questions.
Connections
None.
Tell Me / Show Me
Try It / Solve It
Try It / Solve It
Answers:
These are just a few example queries. Students should run their queries against the database to
check their accuracy.
1. SELECT employee_id, last_name, department_id
FROM employees
WHERE LOWER(last_name) = 'ernst';
SELECT employee_id, CONCAT(first_name, last_name) NAME,
job_id, LENGTH (last_name),
INSTR(last_name, 'i') "Contains 'i'?"
FROM employees
WHERE SUBSTR(job_id, 4) = 'PRES';
SELECT ROUND(45.923,2), ROUND(45.923,0),
ROUND(45.923,-1)
FROM DUAL;
Try It / Solve It
Try It / Solve It
Answers:
2. SELECT UPPER(CONCAT(cd_number, title))
FROM d_cds WHERE cd_number = 94;
SELECT UPPER(cd_number)||UPPER(title)
FROM d_cds WHERE cd_number = 94;
Try It / Solve It
Try It / Solve It
Answers:
6. SELECT TO_DATE('December 15, 1995', 'Month dd, RRRR')
FROM DUAL;
9. There is nothing wrong with the syntax; there is no matching data in the database.
Try It / Solve It
Try It / Solve It
Answers:
10. _C__a. To convert varchar2 to number data
_C__b. To format a date to other than the default format
_C__c. To convert a date such as June 19, 2000 to default format
_N__d. To format a number to appear as currency
_G__e. To substitute a value in a table for null
_CE_f. To do an IF-THEN-ELSE statement
_G__g. To find the first not null expression among a list of expressions
_C__h. To replace a section of a string with another string
_C__i. To format a 20th-century date
_C__j. To present output all in uppercase
_C__k. To find the numeric position of a character in a string
_D__l. To find the last day of the month