TO_DATE

TO_DATE is an SQL function that converts a string into a date.

TO_DATE(string, format)

  • string: This is the input parameter, a character literal representing a date and time. It directly inputs the actual date, time, and year that is needed to be converted.
  • format: This parameter defines the format of the string parameter. It specifies the expected date, time, and year format of the string. The format option allows for variability in the input based on a defined pattern.

Example

SELECT TO_DATE('22-AUG-2020', 'DD-MON-YYYY') AS "Converted_Date" FROM dual;

Output

Converted_Date
--------------
2020-08-22

Explanation

The TO_DATE function in Oracle converts a string value to a DATE value according to the format model specified. The example converts the string ‘22-AUG-2020’ to a DATE value in ‘YYYY-MM-DD’ format.

TO_DATE(text, text) RETURNS date

  • text: This is a string that represents a date. The format should comply with the rules specified in the second parameter of the TO_DATE function.
  • text: This is a format string that dictates how the first parameter (date string) is parsed to construct a date. It allows for parsing dates that are written in different formats.

Example

SELECT TO_DATE('05-22-2021', 'MM-DD-YYYY');

Output

"2021-05-22"

Explanation

The TO_DATE function in PostgreSQL is used for converting a string into a date. In the given example, the input string ‘05-22-2021’ is converted into a date format ‘YYYY-MM-DD’. The output is ‘2021-05-22’.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.