Introduction to PostgreSQL TO_DATE()
PostgreSQL TO_DATE() function is used to convert string literal into date value; basically, to_date function will convert the string into a date. We have given two inputs parameter arguments with the to_date function in PostgreSQL, i.e. text and format; the to_date function is very useful and important in PostgreSQL to convert the string into a date value. To_date function returns the date from function as per we have provided input parameter from the to_date function in PostgreSQL. To_date function is used to handle the input formats which the simple casting cannot convert; this function will interpret input by minimal error checking.
Syntax
Below is the syntax of the to_date function in PostgreSQL.
- to_date (text, format)OR
- to_date (text, text)OR
- to_date (string, format)
Below is the parameter description syntax of the to_date function in PostgreSQL.
- to_date –To_date function is used to convert a date represented in a character string to a date data type.
- Text –Text is the first parameter that was accepted by the to_date function in PostgreSQL. Text is nothing but string argument, which we have converted into the date.
- Format –The second argument is nothing but the input format of the to_date function, which returned the date value. It will define the format of the input string in the form of date parts.
- String –String argument is defined as which string that we have converted into the date. We have also defined a string as text in the to_date function.
Working of PostgreSQL TO_DATE() function
- Below is the working of the to_date function in PostgreSQL.
- The main use of the to_date function in PostgreSQL is to convert string lateral into the date; the to_date function is used to convert the string into date format.
- We have pass two arguments with the to_date function in PostgreSQL, i.e. string and format.
- We can format the date value using the to_date function in PostgreSQL are as follows.
Patterns for formatting date values using to_date function in PostgreSQL.
- Y,YYY – This is defined as a year in four digits separated by a comma. The comma is separated after the first digits.
- YYYY – This format will define as the year in four digits. We have not used any string to divide the year.
- YYY – This format will define the last three digits of the year.
- YY – This format will define the last two digits of the year.
- Y – This format will define the last one digits of the year.
- IYYY – This is defined as ISO numbering of year. It will display the last four or more digits as output.
- IYY –This format of ISO numbering will define the last three digits of the week numbering year.
- IY – This format of ISO numbering will define the last two digits of the week numbering year.
- I – It will define the last digit of ISO 8601 week numbering year.
- MONTH –It is used to define the English month name in uppercase.
- Month – This is defined as a full capitalized English month name.
- month – This is defined full lowercase English month name.
- MON –This is abbreviated uppercase English month name, i.e. JAN, FEB.
- Mon –This is abbreviated capitalized English month name, i.e. Jan, Feb.
- mon – This is abbreviated lowercase English month name, i.e. Jan, feb.
- MM – This is defined as the month number from 0 to 12.
- DAY – This is defined as a full uppercase day name.
- Day – This is defined as a full capitalized day name.
- day – This is defined as the full lowercase day name.
- DY – This is an abbreviated uppercase day name.
- Dy – This is an abbreviated capitalized day name.
- dy – This is an abbreviated lowercase day name.
- DDD – This is defined as the day of the year from (000 to 366).
- DD – This is defined as the day of the month from 01 to 31.
- D –This is defined as the day of the week, Sunday as 1 and Saturday as 7.
- We can convert the date using the to_date function as we specified a format; we can use any format as above in the to_date function.
- If suppose we have to convert the string as 2020 May 20 into date value, we can use the pattern as “DD Mon YYY” or “YYYY Mon dd”.
Examples
Below is an example of the to_date function in PostgreSQL.
Example #1 – Convert the string using the ‘YYYY MM DD’ format
We have converted the ‘2020 05 20’ date using the ‘YYYY MM DD’ format in the below example.
SELECT to_date('2020 05 20', 'YYYY MM DD');
Output:
Example #2 – Convert string using ‘DD MON YYYY’ format
We have converted the ’15 MAY 2020′ date using the ‘DD MON YYYY’ format in the below example.
SELECT to_date('15 MAY 2020', 'DD MON YYYY');
Output:
Example #3 – Convert the string using ‘YYYY Mon DD’ format
In the below example, we have converted the ‘2020 May 20′ date using’YYYY Mon DD’ format.
SELECT to_date('2020 May 20','YYYY Mon DD');
Output:
Example #4 – PostgreSQL to_date gotchas
In the below example, we have passing an invalid date string to the to_date function. After passing invalid date sting then we have converted the string into the date format.
SELECT to_date('2020/05/20', 'YYYY/MM/DD');
Output:
Example #5 – Convert the string using’MMDDYY’ format
We have converted the ‘052020’ date using the ‘MMDDYY’ format in the below example.
SELECT to_date('052020', 'MMDDYY');
Output:
Recommended Articles
This is a guide to PostgreSQL TO_DATE(). Here we discuss the Working of the PostgreSQL TO_DATE() function and Examples along with functions and outputs. You may also look at the following articles to learn more –