Professional Documents
Culture Documents
Labsheet 10
Labsheet 10
IS222
Table of Contents
LAB 10: Single Row Functions ................................................................................................ 2
Objectives: ......................................................................................................................................... 2
Tools/Software: ................................................................................................................................. 2
Concepts & Descriptions: ................................................................................................................ 2
Using Dual Table, Case Manipulation Functions, and Character Manipulation Functions .......................... 2
Using Numeric Functions ............................................................................................................................. 4
Using DATE Functions ................................................................................................................................. 5
Date Conversions to Character Data ............................................................................................................. 6
Number Conversion to Character Data ......................................................................................................... 7
Character Conversion to Number .................................................................................................................. 8
Character Conversion to Date ....................................................................................................................... 8
fxModifier Rules ........................................................................................................................................... 8
Combination Example................................................................................................................................... 8
Functions Pertaining to Null Values ............................................................................................................. 9
NVL Function ............................................................................................................................................... 9
CASE Expression .......................................................................................................................................... 9
Lab Activities: ................................................................................................................................. 10
Deliverables: .................................................................................................................................... 14
LAB 10: Single Row Functions
Objectives:
This lesson covers the following objectives:
• create SELECT Statements using DUAL table
• create SELECT Statements using case manipulation function
• create SELECT Statements using character manipulation functions
• create SELECT Statements using column aliases with functions
• create SELECT Statements using ROUND, TRUNC and MOD
• create SELECT Statements using Date functions
• create SELECT Statements using Date conversions
• create SELECT Statements using Number conversion
• create SELECT Statements using Character conversion
• create SELECT Statements using fxModifier
• create SELECT Statements using functions pertaining to Null values
• create SELECT Statements using conditional expressions
Tools/Software:
To accomplish this session, students should have access to APEX.
Using Dual Table, Case Manipulation Functions, and Character Manipulation Functions
Example Explanation
DUAL Table
SELECT (319/29) + 12 DUAL will be used to learn many of the
FROM DUAL; single-row functions.
In this example the DUAL table is used
to execute a SELECT statement that
contains a calculation.
Example Explanation
SELECT SYSDATE SYSDATE is a date function that returns
FROM dual; the current database server date and
time.
Result:
16-Nov-1999 01-Dec-1999
17-Oct-1995 01-Nov-1995
29-Jan-1997 01-Feb-1997
.. ..
Example Output
SELECT TO_CHAR(hire_date, 'Month dd, YYYY') June 07, 1994
FROM employees;
SELECT TO_CHAR(hire_date, 'fmDay ddth Mon, YYYY') Tuesday 7th Jun, 1994
FROM employees;
SELECT TO_CHAR(hire_date, 'fmDay ddthsp Mon, YYYY') Tuesday, seventh Jun, 1994
FROM employees;
SELECT TO_CHAR(hire_date, 'fmDay, ddthsp "of" Month, Year') Tuesday, seventh of June,
FROM employees; Nineteen Ninety-Four
Example Output
SELECT TO_CHAR(3000, '$99999.99') $3000.00
FROM dual;
Example Output
SELECT TO_NUMBER('5,320', '9,999') AS "Number" 5320
FROM dual;
Example Output
SELECT TO_DATE('May10,1989', 'fxMonDD,YYYY') AS "Convert" 10/May/1989
FROM DUAL;
fxModifier Rules
Example Output
SELECT TO_DATE('Sep 07, 1965', 'fxMon dd, YYYY') AS "Date" 07/Sep/1965
FROM dual;
Combination Example
Example Explanation
SELECT Friday, December 18th, 1987
TO_CHAR(NEXT_DAY(ADD_MONTHS(hire_date,
6), 'FRIDAY'), 'fmDay, Month DDth, YYYY') Step 1: The hire date is going to have
AS "Next Evaluation" six months added to it.
FROM employees
Example Explanation
WHERE employee_id=100; Step 2: The first Friday following the
day returned at Step 1 will be
identified.
NVL Function
• The NVL function converts a null value to a known value of a fixed data type, either
date, character, or number.
• The data types of the null value column and the new value must be the same.
Example Explanation
SELECT country_name, NVL(internet_extension, 'None') Null values are replaced with the text
AS "Internet extn" 'None'.
FROM wf_countries
WHERE location = 'Southern Africa'
ORDER BY internet_extension DESC;
CASE Expression
• The CASE expression basically does the work of an IF-THEN-ELSE statement.
• Data types of the CASE, WHEN, and ELSE expressions must be the same.
Example Explanation
SELECT last_name, The query checks the department_id.
CASE department_id IF it is 90, then return 'Management'
WHEN 90 THEN 'Management' IF it is 80, then return 'Sales'
WHEN 80 THEN 'Sales' IF it is 60, then return 'It'
WHEN 60 THEN 'It' ELSE return 'Other dept.'
ELSE 'Other dept.'
END AS "Department"
FROM employees;
Lab Activities:
1. Write a SQL statement to retrieve all columns from employees table with last name of
‘abel’, regardless whether the letters are in upper or lower cases.
SELECT *
FROM employees
WHERE LOWER(last_name) = 'abel';
2. Write a SQL statement using employees table to display the first name column in all
capital letters, the last name column in all small letters, and email column with its first
letter capitalized.
SELECT
UPPER(first_name) AS First_Name,
LOWER(last_name) AS Last_Name,
INITCAP(email) AS Email
FROM employees;
3. Write a SQL statement using employees table to display a derived column with first
name and last name concatenated, a derived column with only the first 3 letters of
first name, and a derived column with the length of first name.
SELECT
CONCAT(first_name, last_name) AS FULL_NAME,
SUBSTR(first_name, 1, 3) ,
LENGTH(first_name)
FROM employees;
4. Write a SQL statement using employees table to display the first name column, the
index value if letter ‘n’ is contained/found in first name, the first name after replacing
‘n’ with ‘*’, and the first name after deleting ‘n’ in its beginning or ending.
SELECT
first_name,
INSTR(first_name, 'n') AS index_of_n,
REPLACE(first_name, 'n', '*') AS name_with_asterisk,
TRIM(BOTH 'n' FROM first_name) AS name_without_n
FROM employees;
5. Write a SQL statement using employees table to display the 3rd of a salary, the 3rd of a
salary rounded with no decimal part, and the 3rd of a salary truncated with no decimal
part.
SELECT
salary / 3 ,
ROUND(salary / 3, 0) ,
TRUNC(salary / 3, 0)
FROM employees;
6. Considering that all employees are still working in the company, write a SQL
statement using employees table to display the first name, date hired, the number of
days he/she is employed, and the number of years he/she is employed.
SELECT
first_name,
hire_date,
TRUNC(SYSDATE - hire_date) AS days_employed,
EXTRACT(YEAR FROM SYSDATE) - EXTRACT(YEAR FROM hire_date) AS
years_employed
FROM employees;
7. Write a SQL statement using employees table to display the first name, date hired, the
date 6 months after date hired, and date hired in full string format (e.g. Thursday,
October 18, 2018).
SELECT
first_name,
hire_date,
ADD_MONTHS(hire_date, 6) AS six_months_after_hire,
TO_CHAR(hire_date, 'Day, Month DD, YYYY') AS hire_date_string
FROM employees;
8. Write a SQL statement using employees table to display first name and date hired of
those who were hired from 1995 to present and sort them by date hired.
SELECT
first_name,
hire_date
FROM employees
WHERE hire_date >= DATE '1995-01-01'
ORDER BY hire_date;
Deliverables:
At the end of this session, students are expected to have written all the SQL statements in the
activity with correct output list.