Databases

Oracle to_date() Function

This tutorial aims to show you how to use the to_date() function in Oracle to perform that.
Captain Salem 3 min read
Oracle to_date() Function

Dates and strings are some of the most common data types you will interact with when working with databases. It is, therefore, no surprise that you will encounter a scenario where you need to convert a given string literal into a date type.

Let us dive in.

Orale to_date() Function

The to_date() function in Oracle allows you to convert a given string date literal into a date type.

The function syntax can be expressed in the snippet below:

TO_DATE(char [, fmt [, 'nlsparam' ] ])

The function accepts three main arguments:

  1. char – the first argument is the char, which defines the input date string. The input value can be CHAR, VARCHAR2, NCHAR, or NVARCHAR2.
  2. fmt – The second parameter is fmt. This parameter defines the datetime model format of the input value. You can skip this parameter if the input value follows the default format of DD-MON-YY, for example, 01-JAN-2024.

NOTE: If the format is Julian, represented as J, then the input value must be an integer type.

The input value can be comprised of the following formats.

Format ModelDescription
YYYY4-digit year (e.g., 2024)
YY2-digit year (e.g., 24 for 2024)
MM2-digit month (01 to 12)
MONAbbreviated month name (e.g., Jan, Feb, Mar)
MONTHFull month name (e.g., January, February)
DD2-digit day of the month (01 to 31)
HHHour in 12-hour format (01 to 12)
HH12Hour in 12-hour format (01 to 12)
HH24Hour in 24-hour format (00 to 23)
MIMinute (00 to 59)
SSSecond (00 to 59)
AMMeridian indicator (AM or PM)
PMMeridian indicator (AM or PM)
ADAnno Domini (AD) or Before Christ (BC)
BCBefore Christ (BC)
TZDTime Zone Abbreviation (e.g., PST, GMT)
TZOTime Zone Offset (e.g., -08:00)
DYAbbreviated day name (e.g., Sun, Mon, Tue)
DAYFull day name (e.g., Sunday, Monday)
WWWeek of the year (1 to 52/53)
IWISO week of the year (1 to 52/53)
DDDDay of the year (1 to 366)
YYYY4-digit year (e.g., 2024)
YY2-digit year (e.g., 24 for 2024)
CCCentury (e.g., 20 for the 21st century)
SYYYY4-digit year (e.g., 2024)
SYY2-digit year (e.g., 24 for 2024)
SYYYY4-digit year including century (e.g., 2024)
SYY2-digit year including century (e.g., 24 for 2024)
  1. nlsparam – Finally, the nlsparam parameter is used to specify the language for data and month in the string. The format is NLS_DATE_FORMAT = language. Keep in mind that this is an optional parameter, and Oracle will default to the default language of your session.

Example Function Usage

The following are examples of the to_date function in the Oracle database.

Example 1 – Basic Conversion

The example below demonstrates how to use the to_date function to convert a given character string to a date.

select to_date('January 10 2024', 'Month dd, YYYY')
from dual;

In this case, we convert the given date string to a date type using the Month dd, YYYY format.

The resulting value is shown:

TO_DATE('JANUARY102023','MONTHDD,YYYY')
2024-01-10

Example 2 – Convert Date and Time to Date Type

The second example illustrates how to convert a given character string to date.

select to_date('January 10, 2024, 1:03', 'Month dd, YYYY, HH:MI P.M.')
from dual;

In this case, we need to specify the time format as HH:MI P.M.

The resulting output is as shown:

TO_DATE('JANUARY10,2024,1:03','MONTHDD,YYYY,HH:MIP.M.')
2024-01-10 01:03:00

Example 3 – Specifying the Language Parameter

Consider the example below demonstrating how to use the to_date function with the nls_date_language parameter.

select to_date('January 10, 2024, 1:03', 'Month dd, YYYY, HH:MI P.M.', 'nls_date_language=American')
from dual;

This should return the value:

2024-01-10 01:03:00

Once we change the session language as:

alter session set nls_territory = 'Australia';

Convert the time.

select to_date('January 10, 2024, 1:03', 'Month dd, YYYY, HH:MI P.M.', 'nls_date_language=American')
from dual;

Ending

In this post, we explore the usage of the to_date function in the Oracle database to convert a given input date literal to date type.

Share
Comments
More from Cloudenv
SQL XOR Operator
Databases

SQL XOR Operator

Let us explore what the XOR operator in SQL does and how we can use it. For demonstration purposes, we will use MySQL as the base database system.
Captain Salem 2 min read

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.