This tutorial is based on examples so it would be easier to understand. Oracle To_Date function converts built-in data types like string of CHAR, VARCHAR2, NCHAR, or NVARCHAR2 to a DATE data type and this way you will be able to do with the value date operations. The date format is not required but then your date value has to be written in way of default database date format. Also the NLS_DATE_FORMAT setting is optional but it gives you an opportunity to export the date value in an another time region style. The syntax of Oracle To_Date is :
TO_DATE (<string>[,<date_format>] [,NLS_DATE_FORMAT]);
The first Oracle To_Date example has date November 20th 2010 written in a string as “2010/11/20“. Behind the date value we have declared the Oracle date format that describes the date value look. Oracle DBA recommends to use always the date format even it is optional. Using a date format your date value will be converted always correctly and the default database date format can be changed without affecting the existing code or SQL queries.
SELECT TO_DATE ('2010/11/20','yyyy/mm/dd') AS my_date FROM dual;
The second example is written to use the same date value as above only with in lightly different format. The example above had from left to right first number of years (YYYY) then months (MM) and in the last days (DD). The following example has first number months (MM) then days (DD) and the last comes years (YYYY). Also the date format has changed among of the date value and reflects the date value structure.
SELECT to_date ('11/20/2010','mm/dd/yyyy') AS my_date FROM dual;
We have been using “/“-characters to separate date numbers on the last two examples but using them is not required and the next example shows that the date value can contain only numbers. The value will be converted correctly while the date format is declared to reflect the value. The date has still remained to November 20th 2010.
SELECT to_date ('20112010','ddmmyyyy') AS my_date FROM dual;
The Oracle To_Date function is quite flexible and you can leave away even some date values. The following example has only month and year numbers and once again the Oracle date format allows the To_Date function to do converting correctly. Since the day number is missing the date will become first day of month and the SQL query output has November 1st 2010.
SELECT to_date ('11/2010','mm/yyyy') AS my_date FROM dual;
Also you can convert your string to date type using only a year number as on the following example. When you take a look at the last example that had no day number and the Oracle database returned the first day of a month but leaving away the month number you will receive the current month and not the first month of year as you may guessed. The example above has two columns where the first is converted date using year 2010 number and the second column has current date the April 15th 2011. Take a look at the converted column “MY_DATE” the date is April 1st 2010.
SELECT to_date ('2010','yyyy') AS my_date, SYSDATE FROM dual;
The Oracle date format allows to import date values from different look to the database default format so always use the Oracle date format among with Oracle To_Date function. In addition, you can find a table below for Oracle date format elements to do conversions.
Oracle Date and Time Format Elements
||TO_* datetime functions?||Description|
|Yes||AD indicator with or without periods.|
|Yes||Meridian indicator with or without periods.|
|Yes||BC indicator with or without periods.|
For example, 2002 returns 21; 2000 returns 20.
|D||Yes||Day of week (1-7). This element depends on the NLS territory of the session.|
|DAY||Yes||Name of day.|
|DD||Yes||Day of month (1-31).|
|DDD||Yes||Day of year (1-366).|
|DL||Yes||Returns a value in the long date format, which is an extension of the Oracle Database
|DS||Yes||Returns a value in the short date format. Makes the appearance of the date components (day name, month number, and so forth) depend on the
|DY||Yes||Abbreviated name of day.|
|E||Yes||Abbreviated era name (Japanese Imperial, ROC Official, and Thai Buddha calendars).|
|EE||Yes||Full era name (Japanese Imperial, ROC Official, and Thai Buddha calendars).|
|FF [1..9]||Yes||Fractional seconds; no radix character is printed. Use the X format element to add the radix character. Use the numbers 1 to 9 after FF to specify the number of digits in the fractional second portion of the datetime value returned. If you do not specify a digit, then Oracle Database uses the precision specified for the datetime data type or the data type’s default precision. Valid in timestamp and interval formats, but not in
|FM||Yes||Returns a value with no leading or trailing blanks.See Also: Additional discussion on this format model modifier in the Oracle Database SQL Language Reference|
|FX||Yes||Requires exact matching between the character data and the format model.See Also: Additional discussion on this format model modifier in the Oracle Database SQL Language Reference|
|Yes||Hour of day (1-12).|
|HH24||Yes||Hour of day (0-23).|
|IW||Week of year (1-52 or 1-53) based on the ISO standard.|
|Last 3, 2, or 1 digit(s) of ISO year.|
|IYYY||4-digit year based on the ISO standard.|
|J||Yes||Julian day; the number of days since January 1, 4712 BC. Number specified with J must be integers.|
|MM||Yes||Month (01-12; January = 01).|
|MON||Yes||Abbreviated name of month.|
|MONTH||Yes||Name of month.|
|Yes||Meridian indicator with or without periods.|
|Q||Quarter of year (1, 2, 3, 4; January – March = 1).|
|RM||Yes||Roman numeral month (I-XII; January = I).|
|RR||Yes||Lets you store 20th century dates in the 21st century using only two digits.See Also: “The RR Datetime Format Element”|
|RRRR||Yes||Round year. Accepts either 4-digit or 2-digit input. If 2-digit, provides the same return as RR. If you do not want this functionality, then enter the 4-digit year.|
|SSSSS||Yes||Seconds past midnight (0-86399).|
|TS||Yes||Returns a value in the short time format. Makes the appearance of the time components (hour, minutes, and so forth) depend on the
|TZD||Yes||Daylight saving information. The TZD value is an abbreviated time zone string with daylight saving information. It must correspond with the region specified in TZR. Valid in timestamp and interval formats, but not in
|TZH||Yes||Time zone hour. (See
|TZM||Yes||Time zone minute. (See
|TZR||Yes||Time zone region information. The value must be one of the time zone region names supported in the database. Valid in timestamp and interval formats, but not in
|WW||Week of year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year.|
|W||Week of month (1-5) where week 1 starts on the first day of the month and ends on the seventh.|
|X||Yes||Local radix character.Example:
|Y,YYY||Yes||Year with comma in this position.|
|Year, spelled out;
|Yes||Last 3, 2, or 1 digit(s) of year.||YYYY