Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Kingdom of Saudi Arabia

Ministry of Higher Education


King Faisal University
College of Computer Sciences &
Information Technology

IS222 – DATABASE CONCEPTS


AND DESIGN
Lab sheet

Lab 10: Single Row Functions

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.

Concepts & Descriptions:

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.

Case Manipulation Functions


SELECT last_name LOWER(column | expression) converts
FROM employees alpha characters to lower-case.
WHERE LOWER(last_name) = 'abel';

SELECT last_name UPPER(column | expression) converts


FROM employees alpha characters to upper-case.
WHERE UPPER(last_name) = 'ABEL';

SELECT last_name INITCAP(column | expression) converts


FROM employees alpha character values to uppercase for
WHERE INITCAP(last_name) = 'Abel'; the first letter of each word.
Example Explanation
Character Manipulation Functions
SELECT CONCAT('Hello', 'World') CONCAT: Joins two values together.
FROM DUAL; Result is ‘HelloWorld’.

SELECT CONCAT(first_name, last_name) Result is ‘EllenAbel’, ‘CurtisDavies’,


FROM employees; etc.

SELECT SUBSTR('HelloWorld',1,5) SUBSTR: Extracts a string of a


FROM DUAL; determined length.
Result is ‘Hello’.

SELECT SUBSTR('HelloWorld', 6) Result is ‘World’.


FROM DUAL;

SELECT SUBSTR(last_name,1,3) Result is ‘Abe’, ‘Dav’, etc.


FROM employees;

SELECT LENGTH('HelloWorld') LENGTH: Shows the length of a string


FROM DUAL; as a number value.
Result is 10.

SELECT LENGTH(last_name) Result is 4, 6, etc.


FROM employees;

SELECT INSTR('HelloWorld', 'W') INSTR: Finds the numeric position of


FROM DUAL; the specified character(s).
Result is 6.

SELECT last_name, INSTR(last_name, 'a') Abel 0


FROM employees; Davies 2

SELECT LPAD('HelloWorld',15, '-') LPAD: Pads the left side of a character


FROM DUAL; string, resulting in a right-justified
value.
Result is ‘-----HelloWorld’.

SELECT LPAD(last_name, 10,'*') ******Abel


FROM employees; ****Davies

SELECT RPAD('HelloWorld',15, '-') RPAD: Pads the right-hand side of a


FROM DUAL; character string, resulting in a left-
justified value.
Result is ‘HelloWorld-----’.
SELECT RPAD(last_name, 10,'*') Abel******
FROM employees; Davies****
Example Explanation
SELECT TRIM(LEADING 'ab' FROM 'abcba') TRIM: Removes all specified characters
FROM DUAL; from either the beginning, the end, or
both beginning and end of a string.
Result is ‘cba’.
SELECT TRIM(TRAILING 'a' FROM 'abcba') abcb
FROM DUAL;

SELECT TRIM(BOTH 'a' FROM 'abcba') bcb


FROM DUAL;

SELECT REPLACE('JACK and JUE','J','BL') REPLACE: Replaces a sequence of


FROM DUAL; characters in a string with another set of
characters.
Result is ‘BLACK and BLUE’.
SELECT REPLACE('JACK and JUE','J') ACK and UE
FROM DUAL;

SELECT REPLACE(last_name,'a','*') Abel


FROM employees; D*vies
De H**n

Using Column Aliases With Functions


SELECT LOWER(last_name) || user_name is the alias
LOWER(SUBSTR(first_name,1,1))
AS user_name
FROM employees;

SELECT LOWER No alias was used


(last_name)||LOWER(SUBSTR(first_name,1,1))
FROM f_staffs;

Using Numeric Functions


ROUND ( <column1> | <expression> , <decimal_places> )
Example Result
ROUND(45.926) 46
ROUND(45.926, 0) 46
ROUND(45.926, 2) 45.93
ROUND(45.926, -1) 50

TRUNC ( <column1> | <expression> , <decimal_places> )


Example Result
TRUNC (45.926, 2) 45.92
TRUNC (45.926, 0) 45
TRUNC (45.926) 50
The MOD function finds the remainder after one value is divided by another value.
Example Explanation
SELECT country_name, MOD(airports,2) The "Mod Demo" column will show if
AS "Mod Demo" number of airports for each country is an
FROM countries; odd or even number.

Using DATE Functions

Example Explanation
SELECT SYSDATE SYSDATE is a date function that returns
FROM dual; the current database server date and
time.

SELECT last_name, hire_date + 60 Adds 60 days to hire_date.


FROM employees;

SELECT last_name, (SYSDATE – Displays the number of weeks since the


hire_date)/7 employee was hired.
FROM employees;

SELECT employee_id, (end_date – Finds the number of days employee held


start_date)/365 a job, then divides by 365 to display in
AS "Tenure in last job" years.
FROM job_history;

SELECT last_name, hire_date MONTHS_BETWEEN: takes 2 DATE


FROM employees arguments and returns the number of
WHERE MONTHS_BETWEEN calendar months between the 2 dates.
(SYSDATE, hire_date)>240;

SELECT ADD_MONTHS (SYSDATE, 12) ADD_MONTHS: takes 2 arguments, a


AS "Next Year" DATE and a number. Returns a DATE
FROM dual; value with the number argument added
to the month component of the date.

SELECT NEXT_DAY (SYSDATE, 'Saturday') NEXT_DAY: takes 2 arguments, a


AS "Next Saturday" DATE and a weekday and returns the
FROM dual; DATE of the next occurrence of that
weekday after the DATE argument.

SELECT LAST_DAY (SYSDATE) LAST_DAY: takes a DATE argument


AS "End of the Month" and returns the DATE of the last day of
FROM dual; the month for the DATE argument.

SELECT hire_date, ROUND: returns a DATE rounded to


ROUND (hire_date, 'Month') the unit specified by the second
FROM employees
Example Explanation
WHERE department_id=50; argument.

Result:
16-Nov-1999 01-Dec-1999
17-Oct-1995 01-Nov-1995
29-Jan-1997 01-Feb-1997
.. ..

SELECT hire_date, TRUNC: returns a DATE truncated to


TRUNC(hire_date, 'Year') the unit specified by the second
FROM employees argument.
WHERE department_id=50; Result:
16-Nov-1999 01-Jan-2000
17-Oct-1995 01-Jan-1996
29-Jan-1997 01-Jan-1997
.. ..

Date Conversions to Character Data


• Use sp to spell out a number.
• Use th to have the number appear as an ordinal. (1st, 2nd, 3rd, and so on).
• Use an fm element to remove padded blanks or remove leading zeroes from the
output.

Example Output
SELECT TO_CHAR(hire_date, 'Month dd, YYYY') June 07, 1994
FROM employees;

SELECT TO_CHAR(hire_date, 'fmMonth dd, YYYY') June 7, 1994


FROM employees;

SELECT TO_CHAR(hire_date, 'fmMonth ddth, YYYY') June 7th, 1994


Example Output
FROM employees; January 3rd, 1990

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

SELECT TO_CHAR(SYSDATE, 'hh:mm') 02:07


FROM dual;
SELECT TO_CHAR(SYSDATE, 'hh:mm pm') 02:07 am
FROM dual;

SELECT TO_CHAR(SYSDATE, 'hh:mm:ss pm') 02:07:23 am


FROM dual;

Number Conversion to Character Data

Example Output
SELECT TO_CHAR(3000, '$99999.99') $3000.00
FROM dual;

SELECT TO_CHAR(4500, '99,999') 4,500


FROM dual;

SELECT TO_CHAR(9000, '99,999.99') 9,000.00


Example Output
FROM dual;

SELECT TO_CHAR(4422, '0009999') 0004422


FROM dual;

Character Conversion to Number

Example Output
SELECT TO_NUMBER('5,320', '9,999') AS "Number" 5320
FROM dual;

SELECT last_name, TO_NUMBER(bonus, '9999') AS "Bonus" Zlotkey 1500


FROM employees Abel 1700
WHERE department_id = 80; Taylor 1250

Note: Try removing one ‘9’ and see


the error since bonus has 4
characters.

Character Conversion to Date

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;

SELECT TO_DATE('July312004', 'fxMonthDDYYYY') AS "Date" 31/Jul/2004


FROM DUAL;

SELECT TO_DATE('June 19, 1990','fxMonth dd, YYYY') AS "Date" 19/Jun/1990


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.

Functions Pertaining to Null Values

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;

SELECT last_name, NVL(commission_pct, 0)*250 The commission_pct column in the


AS "Commission" employees table contains null values.
FROM employees The NVL function is used to change
WHERE department_id IN(80,90); the null to zero before arithmetic
calculations.

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.

You might also like