Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 3630 Database Design and Implementation

Similar presentations


Presentation on theme: "CS 3630 Database Design and Implementation"— Presentation transcript:

1 CS 3630 Database Design and Implementation

2 SQL-Plus Prompt Prompt Prompt Assignment 8 Prompt Name: Qi Yang Prompt 1. Prompt List all rooms sorted by price in descending order. Select * From Room Order by Price desc; pause

3 Oracle Date Formats Default Date Format SQL> set pagesize 20
dd-Mon-yy -- 30-Mar-15 Select * From Booking; SQL> set pagesize 20 -- initial size: 11

4 Oracle Date Formats Other Date Format Day ddd: day of the year
d : day of the week: Sun (1), Mon (2), ... dy : mon, tue, (all lower case) Dy : Mon, Tue (Init cap) DY : MON, TUE (all caps)

5 Oracle Date Formats Other Date Format Month
Month: name of month (char(9)) Mon : Three chars mm : Year Year: Year spelled out: Two Thousand Fifteen yyyy: 4 digits yyy: 3 digits yy: 2 digits y: 1 digit

6 Oracle Date Formats Other Date Format Hour HH (HH12): 01 - 12
HH AM : with AM/PM Minute MI : Second SS : Time is included in date Default values: 00:00:00 (HH24)

7 Oracle Function To_Date
Insert into booking Values ('H01', 'G01003', To_date(' ', 'mm-dd-yyyy'), '9-Apr-16', 'R001'); -- Not needed if in standard format

8 Oracle Function To_Char
-- Will display Apr Select guest_no, To_Char(Date_from, 'Mon-dd-yyyy') From booking; -- Will display select to_char(date_from, 'mm/dd/yyyy'), to_char(date_to, 'mm/dd/yyyy') from booking; -- Will display Apr Select guest_no, To_Char(Date_from, 'Mon dd yyyy')

9 Formatting col "Guest No" format a9 col "Date From" format a15 set pagesize 20 Select guest_no "Guest No", To_Char(Date_from, 'Mon-dd-yyyy') "Date From" From booking; pause select to_char(date_from, 'mm-dd-yyyy') "Date From", to_char(date_to, 'mm-dd-yyyy') "Date To" from booking; Select guest_no "Guest No", To_Char(Date_from, 'Mon dd yyyy') "Date From"

10 Comparing Dates Select guest_no "Guest No",
To_Char(Date_from, 'Mon-dd-yyyy') "Date From" From booking Where Date_From >= To_Date('05-May-2005'); pause Where Date_From >= '05-May-2005'; Where Date_From between To_Date('12-Apr-2005') and To_Date('30-Apr-2005'); Do we need to_date for standard format?

11 Table Dual SQL> Desc Dual Name Null? Type
DUMMY VARCHAR2(1) SQL> Select * from dual; DUMMY X

12 Function SysDate How to display date/time in SQLPlus? Select SysDate
From Dual; Select to_char(SysDate, 'Mon-dd-yyyy hh:mi:ss')

13 Select Clause Select SysDate From Booking;
Select 'The system date function', SysDate From Booking Where Hotel_No = 'H01';

14 Date Functions -- next day Select sysdate + 1 from dual;
-- previous month select add_months(sysdate, -1) -- last_day: last day of the month select last_day(sysdate) -- next_day: the next Wednesday select next_day(sysDate, 'Wednesday') From dual; -- Months_Between select months_between(sysdate, add_months(sysdate, 3))

15 Date Functions -- last month of the year
select add_months(sysdate, 12 - to_char(sysdate, 'mm')) from dual; -- to_number select add_months(sysdate, 12 - to_Number(to_char(sysdate, 'mm'))) -- last day of the year select last_day( add_months(sysdate, 12 - to_char(sysdate, 'mm')) ) From dual; -- first month of the year Select add_months(sysdate, 1 - to_char(sysdate, 'mm')) -- first day of the year -- need to be on the same line Select add_months(sysdate, 1 - to_char(sysdate, 'mm')) - to_char(add_months(sysdate, 1 - to_char(sysdate, 'mm')), 'dd') + 1

16 Date and String The latest (largest) Date_To Select Max(Date_To)
From Booking; Select To_Char(Max(Date_To), 'dd Mon yyyy') -- Incorrect! Select Max(To_char(Date_To, 'dd Mon yyyy'))

17 Comparing Dates Bookings for April 2005 Select hotel_no, guest_no,
to_char(date_from, 'dd-mon-yyyy'), to_char(date_to, 'dd-mon-yyyy'), room_no From Booking Where Date_from between '1-Apr-05' and '30-Apr-05' Or Date_to between '1-Apr-05' and '30-Apr-05'; -- Correct?

18 Comparing Dates Bookings during April 2005 Select * From Booking
Where Date_from between '1-Apr-05' and '30-Apr-05' Or Date_to between '1-Apr-05' and '30-Apr-05' Or Date_from < '1-Apr-05' and Date_To > '30-Apr-05'; -- bookings started before apr 2005 and ended after apr 2005

19 Comparing Dates Bookings during April 2005 (better solution!) Select *
From Booking Where not Date_from > '30-Apr-05' and not Date_to < '1-Apr-05';

20 Comparing Dates Bookings for April 2005 Select * From Booking
Where Date_from <= '30-Apr-05' and Date_to >= '1-Apr-05' -- Same as -- Where not Date_from > '30-Apr-05' -- and not Date_to < '1-Apr-05';

21 Comparing Dates Bookings for April 2005 Select * From Booking
Where not (Date_from > '30-Apr-05' Or Date_to < '1-Apr-05') -- DeMorgan’s Law Where not Date_from > '30-Apr-05' and not Date_to < '1-Apr-05';

22 Comparing Dates Bookings this year Select * From Booking
Where To_char(Date_from, 'yyyy') = To_Char(SysDate, 'yyyy') Or To_Char(Date_to, 'yyyy') = To_Char(SysDate, 'yyyy'); -- Assuming no bookings are more than one year -- Assuming bookings could be longer than one year Or To_Char(Date_to, 'yyyy') = To_Char(SysDate, 'yyyy') or to_number(To_char(Date_from, 'yyyy')) < to_number(To_Char(SysDate, 'yyyy')) and to_number(To_Char(Date_to, 'yyyy')) > to_number(To_Char(SysDate, 'yyyy'))

23 Bookings of the current month (of the current year)
-- Bookings could be longer than one month Select * From Booking Where Date_From <= Last_Day(SysDate) and Date_To >= (SysDate - To_Number(To_Char(SysDate, 'dd')) + 1); No First_Day

24 Bookings of the current month (of the current year)
-- Bookings could be longer than one month Select * From Booking Where Date_From <= Last_Day(SysDate) and Date_To > Last_Day(Add_Months(SysDate, -1)); No First_Day

25 Bookings of the previous/next month
-- Bookings could be more than one month Select * From Booking Where Date_From <= Last_Day(Add_Months(SysDate, -1)) and Date_To >= ((Add_Months(SysDate, -1)) - To_Number(To_Char(Add_Months(SysDate, -1), 'dd')) + 1);

26 Bookings of the previous/next month
-- Bookings could be more than one month Select * From Booking Where Date_From <= Last_Day(Add_Months(SysDate, -1)) and Date_To > Last_Day(Add_Months(SysDate, -2));

27 Schedule Project Phase II Friday, April 7 Assignment 8 Due Friday, April 1, by 5 PM

28 Sample SQL Script File No Commit in the query SQL file!
Style Sample SQL Script File No Commit in the query SQL file!


Download ppt "CS 3630 Database Design and Implementation"

Similar presentations


Ads by Google