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