0% found this document useful (0 votes)
233 views3 pages

DP 4 1 Practice DONE

Uploaded by

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

DP 4 1 Practice DONE

Uploaded by

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

Database Programming with SQL

4-1: Case and Character Manipulation


Practice Activities
Objectives
• Select and apply single-row functions that perform case conversion and/or character
manipulation
• Select and apply character case-manipulation functions LOWER, UPPER, and INITCAP in a
SQL query
• Select and apply character-manipulation functions CONCAT, SUBSTR, LENGTH, INSTR,
LPAD, RPAD, TRIM, and REPLACE in a SQL query
• Write flexible queries using substitution variables

Vocabulary
Identify the vocabulary word for each definition below.

Dummy table used to view results from functions and calculations


DUAL
The arrangement of data for storage or display.
Format
Converts alpha character values to uppercase for the first letter of
INITCAP each word, all other letters in lowercase.
Functions that accept character data as input and can return both
Character functions character and numeric values.
Removes all specified characters from either the beginning or the
TRIM ending of a string.
A symbol that represents a quantity or a relationship between
Expression quantities
Functions that operate on single rows only and return one result
Single-row functions per row
Converts alpha characters to upper case
UPPER
Raw data entered into the computer
Input
CONCAT Concatenates the first character value to the second character
value; equivalent to concatenation operator (||).
Data that is processed into information
Output
Converts alpha character values to lowercase.
LOWER
Pads the left side of a character, resulting in a right-justified value
LPAD

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.
Returns specific characters from character value starting at a
SUBSTR specific character position and going specified character positions
long
Replaces a sequence of characters in a string with another set of
REPLACE characters.
Returns the numeric position of a named string.
INSTR
Returns the number of characters in the expression
LENGTH
Pads the right-hand side of a character, resulting in a left- justified
RPAD value.

Try It / Solve It
1. Using the three separate words “Oracle,” “Internet,” and
“Academy,” use one command to produce the following output:

The Best Class SELECT CONCAT('Oracle ', 'Internet')||


Oracle Internet Academy 'Academy' AS "The Best Class"
FROM DUAL;
2. Use the string “Oracle Internet Academy” to produce the following output:

The Net SELECT SUBSTR


net ('Oracle Internet Academy', 13,3) AS
"The Net" FROM DUAL;
3. What is the length of the string “Oracle Internet Academy”?
SELECT LENGTH('Oracle Internet Academy') AS "Length" FROM DUAL; Długość tego string to 23

4. What’s the position of “I” in “Oracle Internet Academy”?


SELECT INSTR('Oracle Internet Academy', 'I')AS "Position" FROM DUAL; Pozycja ‘I’ to 8

5. Starting with the string “Oracle Internet Academy”, pad the string to
create ****Oracle****Internet****Academy****
SELECT LPAD('Oracle',10,'*')||LPAD('Internet',12,'*')||RPAD(LPAD('Academy',11,'*'),15,'*')
AS "LPAD" FROM DUAL;
6. Starting with the string “Oracle Internet Academy”, pad the string to produce:
Oracle$$$Internet$$$Academy
SELECT RPAD('Oracle',9,'$')||RPAD('Internet',11,'$')||'Academy' AS "RPAD" FROM DUAL;

7. Using the string ‘Oracle Internet Academy’, produce the output shown using the REPLACE
function.

The Best Class


SELECT REPLACE
Oracle 2013-2014 Academy
('Oracle Internet Academy', 'Internet','2013-2014')
AS "The Best Class" FROM DUAL;

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.

2
8. List the order date and the order total from the Global Fast Foods F_ORDERS table. Name the
order total as TOTAL, and fill in the empty spaces to the left of the order total with $.
SELECT order_date, LPAD(ORDER_TOTAL,10,'$')AS "TOTAL"
FROM f_orders;
9. Write a query that will output a column called “ADDRESS” which has the following information:
ZOE TWEE 1009 OLIVER AVENUE BOSTON, MA 12889. Use the Global Fast Foods
F_CUSTOMERS table.
SELECT UPPER(first_name) ||' '|| UPPER(last_name) ||' '|| UPPER(address)||' ' ||UPPER(city) ||', '||UPPER(state)||' '||zip
AS "ADDRESS" FROM f_customers WHERE id = 456;
10. Write a query to return the first character of the first name concatenated to the last_name, the
salary, and the department id for employees working in department 20. Give the first expression
an alias of Name. Use the EMPLOYEES table. Change the query to use a substitution variable
instead of the hard coded value 20 for department id. Run the query for department 30 and 50
without changing the original where-clause in your statement.
SELECT CONCAT( SUBSTR(first_name, 1, 1) , last_name) "Name", salary, department_id FROM employees WHERE department_id = 20;
SELECT CONCAT( SUBSTR(first_name, 1, 1) , last_name) "Name", salary, department_id FROM employees WHERE department_id = :dptid;
11. Using a substitution variable for the department name, write a query listing department id,
department name, and location id for departments located in the_department_of_your_choice.
Use the DEPARTMENTS table. Note: All substitution variables in OAE are treated as character
strings, so no quotes (‘ ‘) are needed.
SELECT department_id as "departmentid", department_name as "departmentname", location_id as "location id"
FROM departments WHERE LOWER(department_name) = LOWER(:the_department_of_your_choice);
12. Write a query that returns all the employee data depending on the month of their hire date. Use
the EMPLOYEES table. The statement should return the month part of the hiredate which is then
compared to an abbreviated month (JAN, FEB, MAR) passed into the query via a substitution
variable.
SELECT first_name, last_name, hire_date, TO_CHAR(hire_date, 'MON') as "Month"
FROM employees WHERE TO_CHAR(hire_date, 'MON') = UPPER(:whichmonth);

1TP
Tobiasz
Burzyński

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