280 likes | 294 Views
Learn about the various date formats in Oracle, including default formats and other formats like day, month, year, hour, minute, and second. Explore functions like TO_DATE and TO_CHAR, and how to compare dates in SQL queries.
E N D
Oracle Date Formats • Default Date Format dd-Mon-yy -- 30-Mar-15
Oracle Date Formats Other Date Format Day ddd: day of the year dd : 01-31 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)
Oracle Date Formats Other Date Format Month Month: name of month (char(9)) Mon : Three chars mm : 01 - 12 Year Year: Year spelled out: Two Thousand Fifteen yyyy: 4 digits yyy: 3 digits yy: 2 digits y: 1 digit
Oracle Date Formats Other Date Format Hour HH (HH12): 01 - 12 HH24 : 00 - 23 HH AM : with AM/PM Minute MI : 00 - 59 Second SS : 00 - 59 Time is included in date Default values: 00:00:00 (HH24)
Oracle Function To_Date insert into Booking values ('H01', 'G01003', '25-Apr-2006', to_date('14-May-2006 9:20:33', 'dd-Mon-yyyy hh:mi:ss'), 'R001'); -- Not needed if in standard format
Oracle Function To_Char Select to_char(date_to, 'Mon-dd-yyyy hh:mi:ss') from booking;
Oracle Function To_Char -- Will display Apr-25-2004 Select guest_no, To_Char(Date_from, 'Mon-dd-yyyy') From booking; -- Will display 04-25-2004 select to_char(date_from, 'mm/dd/yyyy'), to_char(date_to, 'mm/dd/yyyy') from booking; -- Will display Apr 25 2004 Select guest_no, To_Char(Date_from, 'Mon dd yyyy') From booking;
Date or String? col "Guest No" format a9 col "Date From" format a15 Select guest_no "Guest No", To_Char(Date_from, 'Mon-dd-yyyy') "Date From", to_char(date_to, 'mm-dd-yyyy') "Date To" From booking Order by "Date From" desc; pause select guest_no "Guest No", to_char(date_from, 'mm-dd-yyyy') "Date From", to_char(date_to, 'mm-dd-yyyy') "Date To" from booking Order by "Date From" desc; pause Select guest_no "Guest No", To_Char(Date_from, 'Mon dd yyyy') "Date From", to_char(date_to, 'mm-dd-yyyy') "Date To" From booking Order by "Date From" desc;
Date or String? Select guest_no "Guest No", To_Char(Date_from, 'Mon-dd-yyyy') "Date From", to_char(date_to, 'mm-dd-yyyy') "Date To" From booking Order by Date_from desc; pause select guest_no "Guest No", to_char(date_from, 'mm-dd-yyyy') "Date From", to_char(date_to, 'mm-dd-yyyy') "Date To" from booking Order by Date_from desc; pause Select guest_no "Guest No", To_Char(Date_from, 'Mon dd yyyy') "Date From", to_char(date_to, 'mm-dd-yyyy') "Date To" From booking Order by Date_from desc;
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 Select guest_no "Guest No", To_Char(Date_from, 'Mon-dd-yyyy') "Date From" From booking Where Date_From >= '05-May-2005'; pause Select guest_no "Guest No", To_Char(Date_from, 'Mon-dd-yyyy') "Date From" From booking Where Date_From between To_Date('12-Apr-2005') and To_Date('30-Apr-2005');
Table Dual SQL> Desc Dual Name Null? Type ----------------------------------------- -------- -------------- DUMMY VARCHAR2(1) SQL> Select * from dual; DUMMY --------- X
Function SysDate • How to display date/time in SQLPlus? Select SysDate From Dual; Select to_char(SysDate, 'Mon-dd-yyyy hh:mi:ss') From Dual;
Select Clause Select SysDate From Booking; Select 'The system date function', SysDate From Booking Select Hotel_No, SysDate From Booking Where Hotel_No = 'H01';
Date Functions -- next day Select sysdate + 1 from dual; -- previous month select add_months(sysdate, -1) from dual; -- last_day: last day of the month select last_day(sysdate) from dual; -- next_day: the next Wednesday select next_day(sysDate, 'Wednesday') From dual; -- Months_Between select months_between(sysdate, add_months(sysdate, 3)) from dual;
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'))) from dual; -- 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')) from dual; -- 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 from dual;
Date and String The latest (largest) Date_To Select Max(Date_To) From Booking; Select To_Char(Max(Date_To), 'dd Mon yyyy') From Booking; -- Incorrect! Select Max(To_char(Date_To, 'dd Mon yyyy')) From Booking;
Comparing Dates Bookings during 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?
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
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';
Comparing Dates Bookings during April 2005 Select * From Booking Where Date_from <= '30-Apr-05' and Date_to >= '1-Apr-05' Order by Date_from; -- Same as -- Where not Date_from > '30-Apr-05' -- and not Date_to < '1-Apr-05';
Comparing Dates Bookings during April 2005 Select * From Booking Where not (Date_from > '30-Apr-05' Or Date_to < '1-Apr-05'); -- DeMorgan’s Law Select * From Booking Where not Date_from > '30-Apr-05' and not Date_to < '1-Apr-05';
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 Select * From Booking Where To_char(Date_from, 'yyyy') = To_Char(SysDate, 'yyyy') 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'));
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);
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));
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);
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));