Database Tutorial

Course Tutorial Site

Site Admin

Date Data Types

without comments

Week 9: Tutorials

DATE Data Types

Learning Outcomes

  • Learn how to create, modify, and remove DATE columns.
  • Learn how perform math with DATE column values.

Dates and timestamps are DATE and TIMESTAMP data types respectively. Their implementation is through complex or real numbers. The integer value represents the date, and the decimal value implements time. The range of dates or timestamps is an epoch. An epoch is a set of possible dates and date-times that are valid in the database server.

The DATE data type in an Oracle database is a date-time value. As such, you can assign a date-time that is accurate to hundredths of a second. The default date format mask in an Oracle database is dd-mon-rr or dd-mon-yyyy. The rr stands for relative date, and the database server chooses whether the date belongs in the current or last century. The yyyy format mask requires that you explicitly assign the four-digit year to dates.

You use the TRUNC function to return only the integer portion of a DATE, which yields midnight of the day. You use the TO_CHAR function with a format mask to see the date value down to the hundredth of a second. The following query returns the integer value generated by the SYSDATE function and the actual time-stamp value generated by the SYSDATE function.

COLUMN midnight FORMAT A22 HEADING "Midnight"
COLUMN now      FORMAT A22 HEADING "Now"
SELECT  TO_CHAR(SYSDATE,'DD-MON-YYYY HH24:MI:SS') AS midnight
,       TO_CHAR(TRUNC(SYSDATE),'DD-MON-YYYY HH24:MI:SS') AS now
FROM dual;

It returns the following values:

Midnight	       Now
---------------------- ----------------------
07-OCT-2018 20:23:29   07-OCT-2018 00:00:00

The behavior of the TRUNC function lets you return yesterday, today, and tomorrow with the following query:

COLUMN yesterday FORMAT A22 HEADING "Yesterday"
COLUMN today     FORMAT A22 HEADING "Today"
COLUMN tomorrow  FORMAT A22 HEADING "Tomorrow"
SELECT TO_CHAR(TRUNC(SYSDATE - 1),'DD-MON-YYYY HH24:MI:SS') AS yesterday
,      TO_CHAR(TRUNC(SYSDATE),'DD-MON-YYYY HH24:MI:SS') AS today
,      TO_CHAR(TRUNC(SYSDATE + 1),'DD-MON-YYYY HH24:MI:SS') AS tomorrow
FROM dual;

It returns the following values:

Yesterday	       Today		      Tomorrow
---------------------- ---------------------- ----------------------
06-OCT-2018 00:00:00   07-OCT-2018 00:00:00   08-OCT-2018 00:00:00

You can find the last day of the month and first day of the month by leveraging the ADD_MONTHS and LAST_DAY builtin functions. They’re powerful functions, as shown in the following query:

COLUMN first_day FORMAT A11 "First Day"
COLUMN LAST_DAY  FORMAT A11 "Last Day"
SELECT  ADD_MONTHS(LAST_DAY(SYSDATE),-1) + 1 AS first_day
,       LAST_DAY(SYSDATE) AS LAST_DAY
FROM    dual;

The first day calculation shows you two key elements of Oracle SQL syntax. One is that you add a day to a date by using an integer. Second, the ADD_MONTHS function lets you pass a negative integer to subtract a month.

It returns the following:

First Day   Last Day
----------- -----------
01-OCT-18   31-OCT-18

You also have the ability of calculating the hours, minutes, and seconds by performing computations on the date value. The following shows you how to calculate the time from midnight to the value returned by the SYSDATE function:

COLUMN midnight FORMAT A22 HEADING "Midnight"
COLUMN now      FORMAT A22 HEADING "Now"
COLUMN HOUR     FORMAT 999 HEADING "Hour"
COLUMN MINUTE   FORMAT 999 HEADING "Minute"
COLUMN SECOND   FORMAT 999 HEADING "Second"
SELECT  TO_CHAR(SYSDATE,'DD-MON-YYYY HH24:MI:SS') AS midnight
,       TO_CHAR(TRUNC(SYSDATE),'DD-MON-YYYY HH24:MI:SS') AS now
,       FLOOR((SYSDATE - TRUNC(SYSDATE)) * 1440 / 60) AS HOUR
,       FLOOR(MOD((SYSDATE - TRUNC(SYSDATE)) * 1440,60)) AS MINUTE
,      (MOD((SYSDATE - TRUNC(SYSDATE)) * 1440,60)
          - FLOOR(MOD((SYSDATE - TRUNC(SYSDATE)) * 1440,60))) * 60 AS SECOND
FROM dual;

The query performs the following calculations:

  • The hour value is calculated by subtracting the truncated current date, or midnight, from the current date-time value. Then, you multiply it by the number of minutes in the day and divide it by 60 minutes. Last, you use the FLOOR function to return the last whole integer value or hour of the day based on a 24-hour day.
  • The minute value is calculated by using the remainder of the same calculation used to find the number of hours. You use the MOD function performs modulo integer math, which returns the remainder after the division. The FLOOR function returns the returns the last whole integer value of minute of the hour.
  • The second value requires more effort. You subtract the last integer value of the minute from the raw minute value, which includes the integer for the last minute plus a decimal value. Then, you multiple the fractional value times 60 to get the number of seconds.

It returns the following:

Midnight	       Now		      Hour Minute Second
---------------------- ---------------------- ---- ------ ------
07-OCT-2018 20:57:19   07-OCT-2018 00:00:00	20     57     19

While adding or subtracting integer values from DATE data types increases or decreases days respectively, you use the INTERVAL function to add or subtract minutes. Here’s an example that returns 15 minutes after midnight of today:

COLUMN fifteen FORMAT A22 HEADING "Fifteen Minutes|After Midnight"
SELECT TO_CHAR(TRUNC(SYSDATE) + INTERVAL '15' MINUTE,'DD-MON-YYYY HH24:MI:SS') AS fifteen
FROM dual;

It shows you the following:

Fifteen Minutes
After Midnight
----------------------
07-OCT-2018 00:15:00

The balance of this tutorial shows you how to create DATE columns and how to cast various data types when implicit casting fails to deliver what you want.

How to Create Tables with DATE Columns

Data Type Conversion Functions

Data type conversion is simply casting, which is the process of taking a variable defined as one data type and changing it to another data type. Implicit casting makes the change for you automatically but only works when the rules are simple and well understood. Explicit casting lets you instruct the programming language how to assign one data type to another when the programming language has no rule that applies to how the data type can be converted implicitly, or without formal programming instructions.

Data type conversion is often done implicitly in SQL and PL/SQL. Unlike other strongly typed programming languages, SQL and PL/SQL implicitly convert types even when there is a potential loss of precision. For example, you can assign a complex number in a NUMBER data type to a SIMPLE_INTEGER data type and lose any values to the right of the decimal point.

Data Type Conversions

The data type conversion functions are useful when you want to make a conversion that requires you to provide instructions. You have to manually convert strings to dates when strings don’t adhere to default format mask conventions. Likewise, some specialized types require you to take specific actions before you can convert data.

The examples that follow focus on demonstrating how to use these functions. You’ll notice that there are no conversions between user-defined object types and standard types. You should include conversion methods in your object type definitions.

CAST Function

CONVERT Function

EXTRACT Function

TO_CHAR Function

TO_CLOB Function

TO_DATE Function

TO_NUMBER Function

Written by michaelmclaughlin

August 14th, 2018 at 12:06 am

Posted in