Temporal Data Part V
Temporal Data Types DATE TIMESTAMP Century Year Month Day Hour Minute Second 2011-04-07 11.07.19 AM TIMESTAMP Includes DATE Plus Fractional Seconds 0 to 9 – default is 6 2011-04-07 11.07.19.123456789 AM
Temporal Functions TO_DATE(string [, format]) TO_TIMESTAMP(string [, format]) TO_CHAR(temporal_data [, format])
Date and Time Formats
Date and Time Formats
Date and Time Formats
Date and Time Formats
Back to the Functions SELECT to_char(sysdate, 'DD-MON-YYYY') FROM dual 18-JUL-2019 SELECT to_date('25-DEC-2019 15:25:23', 'DD-MON-YYYY HH24:MI:SS') FROM dual 12/25/2019 3:25:23 PM
More Functions ADD_MONTHS(date_value, months) CURRENT_DATE Adds months to date_value. Add negative months to subtract. CURRENT_DATE Returns the current date and time as a DATE value SELECT add_months(sysdate, -2) FROM dual 5/18/2019 7:28:26 PM SELECT current_date FROM dual 7/18/2019 7:30:18 PM
More Functions LAST_DAY(date_value) Computes the last day of the month in which the given date_value falls. MONTHS_BETWEEN(later_date, earlier_date) Calculates the number of months between the two given dates. SELECT last_day(sysdate) FROM dual 7/31/2019 7:35:13 PM SELECT months_between(sysdate, to_date('15-JAN-1968', 'DD-MON-YYYY')) FROM dual 618
More Functions NEXT_DAY(date, weekday) SYSDATE Returns the date of the next specified weekday following the given date. SYSDATE Returns the current date and time from the operating system on which the database resides as a DATE value SELECT next_day(sysdate, 'WED') FROM dual 7/24/2019 7:40:06 PM SELECT sysdate FROM dual 7/18/2019 7:41:37 PM
Date Addition A number added to a date is assumed by the system to be a number of days. Convert what you want to add to days Add 20 minutes to sysdate: SELECT sysdate + 7 FROM dual 7/25/2019 7:44:36 PM SELECT sysdate + (20/1440) FROM dual 7/18/2019 8:05:28 PM 60 min/hr * 24 hours in a day = 1440
Rounding & Truncating Dates You can round or truncate a date to a specific minute, hour, day, week, quarter, year or century with ROUND or TRUNC respectively. SELECT round(sysdate, 'HH') FROM dual 7/18/2019 8:00:00 PM SELECT trunc(sysdate, 'HH') FROM dual 7/15/2019 7:00:00 PM
Rounding & Truncating Dates
Rounding & Truncating Dates
Rounding & Truncating Dates SELECT * FROM sprhold WHERE trunc(sprhold_activity_date) = to_date('09-APR-2019', 'DD-MON-YYYY')
Questions?