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

DP 4 3 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)
175 views3 pages

DP 4 3 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

Tobiasz Burzyński 1TP

Database Programming with SQL


4-3: Date Functions
Practice Activities
Objectives
• Select and apply the single-row functions MONTHS_BETWEEN, ADD_MONTHS, NEXT_DAY,
LAST_DAY, ROUND, and TRUNC that operate on date data
• Explain how date functions transform Oracle dates into date data or numeric values
• Demonstrate proper use of the arithmetic operators with dates
• Demonstrate the use of SYSDATE and date functions
• State the implications for world businesses to be able to easily manipulate data stored in date
format

Vocabulary
Identify the vocabulary word for each definition below.

A function that returns the current date and time of the database
SYSDATE server.
Add calendar months to date
ADD_MONTHS
Last day of the month
LAST_DAY
Next day of the date specified
NEXT_DAY
Number of months between due dates
MONTHS_BETWEEN
Try It / Solve It
1. For DJs on Demand, display the number of months between the event_date of the Vigil wedding
and today’s date. Round to the nearest month.
SELECT name, event_date, ROUND(MONTHS_BETWEEN(SYSDATE, event_date)) as "Ile miesiecy?"
FROM wydarzenia WHERE name = 'Vigil wedding';

2. Display the days between the start of last summer’s school vacation break and the day school
started this year. Assume 30.5 days per month. Name the output “Days.”
SELECT TO_DATE('02-Jan-2023', 'dd-Mon-yyyy') - TO_DATE('06-Apr-2023', 'dd-Mon-yyyy')
as "Actual Days", ROUND( MONTHS_BETWEEN(TO_DATE('02-Jan-2023', 'dd-Mon-yyyy'),
TO_DATE('06-Apr-2023', 'dd-Mon-yyyy'))*30.5, 0) as "Days"
FROM dual;
3. Display the days between January 1 and December 31.
SELECT TO_DATE('31-Dec-2023', 'dd-Mon-yyyy') - TO_DATE('01-Jan-2023', 'dd-Mon-yyyy')
as "Actual Days" 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.
4. Using one statement, round today's date to the nearest month and nearest year, and truncate it to
the nearest month and nearest year. Use an alias for each column.
SELECT ROUND(SYSDATE, 'Month') as "najbilzszy miesiac", ROUND(SYSDATE, 'Year')
as "najblizszy rok", TRUNC(SYSDATE, 'Month') as "pierwszy dzien miesiaca",
TRUNC(SYSDATE, 'Year') as "current year's 1st day" FROM dual;
5. What is the last day of the month for June 2005? Use an alias for the output.
SELECT LAST_DAY(To_date('01-Jun-2005', 'dd-Mon-yyyy')) FROM dual;
6. Display the number of years between the Global Fast Foods employee Bob Miller’s birthday and
today. Round to the nearest year.
SELECT first_name, last_name , ROUND(MONTHS_BETWEEN(SYSDATE, birthdate)/12) "ilosc lat"
FROM pracownicy WHERE first_name= 'Bob' && last_name = 'Miller';
7. Your next appointment with the dentist is six months from today. On what day will you go to the
dentist? Name the output, “Appointment.”
SELECT TO_CHAR(ADD_MONTHS(SYSDATE, 6),'dd-Mon-yyyy (DY)') as "Appointment" FROM dual;
8. The teacher said you have until the last day of this month to turn in your research paper. What day
will this be? Name the output, “Deadline.”
SELECT TO_CHAR(LAST_DAY(SYSDATE),'dd-Mon-yyyy (Day)') as "Deadline" FROM dual;
9. How many months between your birthday this year and January 1 next year?
SELECT TO_DATE('03/13/2023','mm/dd/yyyy') "Urodziny w tym roku",
ADD_MONTHS(TO_DATE(''03/13/2023','mm/dd/yyyy'),12) "Urodziny za rok",
TRUNC( ADD_MONTHS(TO_DATE('03/13/2024','mm/dd/yyyy'),12), 'Year') "1 dzien następnego roku" ,
ROUND(MONTHS_BETWEEN( TRUNC( ADD_MONTHS(TO_DATE('03/13/2023','mm/dd/yyyy'),12), 'Year') ,
TO_DATE('03/13/2023','mm/dd/yyyy'))) "Ilosc miesiecy do nastepnego 1 Styczna" FROM dual;
10. What’s the date of the next Friday after your birthday this year? Name the output, “First Friday.”
SELECT TO_DATE('03/13/2023','mm/dd/yyyy') "Urodziny w tym roku",
NEXT_DAY(TO_DATE('03/13/2023','mm/dd/yyyy'), 'Friday') "First Friday" FROM dual;
11. Name a date function that will return a number.
MONTHS_BETWEEN

12. Name a date function that will return a date.


ADD_MONTHS
13. Give one example of why it is important for businesses to be able to manipulate date data?
Powiedzmy, że zapłata firmie zaopatrującą nas w oprogramowanie jest zawsze w 9 dzień miesiąca, ale
w czerwcu wypada w dzień wolny od pracy(czyli banki nie pracują), opłata nie dochodzi i
oprogramowanie antywirusowe przestaje działać. Ktoś się włamuje i mamy straty.
Extension Exercises

1. Using DUAL, write a statement that will convert 86.678 to 86.68.


SELECT ROUND(86.678, 2) FROM dual;
2. Write a statement that will display the DJs on Demand CD titles for cd_numbers 90 and 91 in
uppercase in a column headed “DJs on Demand Collections.”
SELECT UPPER(title) "DJs on Demand Collections" FROM CDs WHERE cd_number IN( 90, 91);

Tobiasz Burzyński 1TP


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.
3. Write a statement that will create computer usernames for the DJs on Demand partners. The
usernames will be the lowercase letters of the last name + the uppercase first letter in the first
name. Title the column “User Passwords.” For example, Mary Smythers would be smythersM.
SELECT LOWER(last_name) || UPPER(SUBSTR(first_name, 1,1) ) "User Passwords"
FROM d_partners;
4. Write a statement that will convert “It’s a small world” to “HELLO WORLD.”
SELECT UPPER(REPLACE( 'It''s a small world' , 'It''s a small', 'hello' )) as converted FROM dual;
5. Write a statement that will remove the “fiddle” from “fiddledeedee” and the “dum” from
“fiddledeedum.” Display the result “fiddledeedeedee” in a column with the heading “Nonsense.”
SELECT REPLACE('fiddledeedum', 'dum') || REPLACE('fiddledeedee', 'fiddle') "Non-sense"FROM dual;
6. Replace every “i” in Mississippi with “$.”
REPLACE('Mississippi ', 'i', '$') "Zmienione"FROM dual;ROM dual;
7. Using DUAL, convert 5332.342 to 5300.
SELECT TRUNC(5332.342, -2) as "Zaokraglone" FROM dual;
8. Using DUAL, convert 3.14159 to 3.14.
SELECT TRUNC(73.892 , 1) as "Truncated" FROM dual;
9. Using DUAL, convert 73.892 to 73.8.
SELECT TRUNC(3.14159 , 2) as "Truncated" FROM dual;
10. What is the next Friday six months from now? Label the column “Future.”
SELECT TO_CHAR( NEXT_DAY( ADD_MONTHS(SYSDATE,6), 'Friday'), 'dd-Mon-yyyy (Day)') "Future"
FROM dual;
11. What is the date 10 years from now? Label the column “Future.”
SELECT TO_CHAR(ADD_MONTHS(SYSDATE, 10*12), 'dd-Mon-yyyy (Day)') "Future" FROM dual;
12. Leap years occur every four years. Remember, 2004 was a leap year. Now create a function that
will show the date of the next leap year as 29-Feb-2008. Label the column “Future.”
SELECT TO_CHAR( LAST_DAY( To_Date('01-Feb-2008', 'dd-Mon-yyyy') ) , 'dd-Mon-yyyy (Day)') "Future"
FROM dual;
13. Write a statement that will find any of the DJs on Demand CD themes that have an “ie” in their
names.
SELECT name FROM themes WHERE name like '%ie%';
14. Write a statement that will return only the DJs on Demand CDs with years greater than 2000 but
less than 2003. Display both the title and year.
SELECT title, year FROM CDs WHERE year > 2000 AND year < 2003;
15. Write a statement that will return the Oracle database employee’s employee ID and his starting
hire dates between January 1, 1997 and today. Display the result ordered from most recently
hired to the oldest.
SELECT employee_id, hire_date FROM employees WHERE hire_date BETWEEN
TO_DATE( '01-Jan-1997', 'dd-Mon-yyyy') AND SYSDATE ORDER BY hire_date DESC;

Tobiasz Burzyński 1TP


3
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