0% found this document useful (0 votes)
352 views4 pages

DP 5 2 Practice

Uploaded by

ACHMAD SUSMONO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
352 views4 pages

DP 5 2 Practice

Uploaded by

ACHMAD SUSMONO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Database Programming with SQL 5-

2: NULL Functions
Practice Activities
Objectives
 Demonstrate and explain the evaluation of a nested function
 List at least four general functions that work with any data type and relate to handling null values
 Explain the use of the COALESCE and the NVL functions
 Explain the use of general functions to deal with null values in data
 Construct and execute a SQL query that correctly applies NVL, NVL2, NULLIF, and
COALESCE single-row functions

Vocabulary
Identify the vocabulary word for each definition below.

NVL Converts nulls to an actual value

COALESCE Returns the first non-null expression in the list

NVL2 Examines the first expression; if the first expression is not null, it
returns the second expression; if the first expression is null, it returns
the third expression
NULLIF Compares two expressions; if they are equal, the function returns null;
if they are not equal, the function returns the first expression

Try It / Solve It

Use aliases to make the output more readable.

1. Create a report that shows the Global Fast Foods promotional name, start date, and end date from the
f_promotional_menus table. If there is an end date, temporarily replace it with “end in two weeks.” If there
is no end date, replace it with today’s date.
SELECT name, start_date, end_date, NVL2(end_date, 'end in two weeks', TO_CHAR( SYSDATE, 'DD-
Mon-YYYY')) as nvl2
FROM f_promotional_menus;

2. Not all Global Fast Foods staff members receive overtime pay. Instead of displaying a null value for these
employees, replace null with zero. Include the employee’s last name and overtime rate in the output. Label
the overtime rate as “Overtime Status”.
SELECT last_name, NVL(overtime_rate,0) as "Overtime Status"
FROM f_staffs;

3. The manager of Global Fast Foods has decided to give all staff who currently do not earn overtime
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
an overtime rate of $5.00. Construct a query that displays the last names and the overtime rate for
each staff member, substituting $5.00 for each null overtime value.
SELECT last_name, TO_CHAR( NVL(overtime_rate,5), '$999.99') as "Overtime Status"
FROM f_staffs;

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
4. Not all Global Fast Foods staff members have a manager. Create a query that displays the employee
last name and 9999 in the manager ID column for these employees.
SELECT last_name, NVL(manager_id,9999) as manager_id
FROM f_staffs;

5. Which statement(s) below will return null if the value of v_sal is 50?
a. SELECT nvl(v_sal, 50) FROM emp;
b. SELECT nvl2(v_sal, 50) FROM emp;
c. SELECT nullif(v_sal, 50) FROM emp;
d. SELECT coalesce (v_sal, Null, 50) FROM emp;

6. What does this query on the Global Fast Foods table return?

SELECT COALESCE(last_name, to_char(manager_id)) as NAME


FROM f_staffs;

Since last_name is not nullable, it will always return last_name. If


last_name would have been nullable and there had been a null last_name
field, it would have fall back to manager_id converted to varchar2.

7.
a. Create a report listing the first and last names and month of hire for all employees in the
EMPLOYEES table (use TO_CHAR to convert hire_date to display the month).
SELECT NVL(first_name,'FNU') , last_name, TO_CHAR(hire_date, 'Month') as "month of
hire"
FROM employees;
First name is nullable, so print first name unknown(FNU in short)

b. Modify the report to display null if the month of hire is September. Use the NULLIF function.
SELECT NVL(first_name,'FNU') , last_name, NULLIF( TO_CHAR(hire_date, 'Month'), 'September') as
"month of hire"
FROM employees;

8. For all null values in the specialty column in the DJs on Demand d_partners table, substitute “No Specialty.” Show
the first name and s
SELECT first_name, NVL(specialty, 'No Specialty') as specialty
FROM d_partners;

2
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.

You might also like