PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
Home / PostgreSQL Functions / PostgreSQL TO_DATE Function: Convert String to Date

PostgreSQL TO_DATE Function: Convert String to Date

Summary: this tutorial introduces you to the PostgreSQL to_date function that helps you convert a string to a date.

Introduction to the PostgreSQL to_date function

The to_date() function converts a string literal to a date value. The following illustrates the syntax of the to_date() function:

1
to_date(text,format);

The to_date() function accepts two string arguments. The first argument is the string that you want to convert to a date. The second one is the input format. The to_date() function returns a date value.

See the following example:

1
SELECT to_date('20170103','YYYYMMDD');

The output shows:

1
2
3
  to_date
------------
2017-01-03

In this example, the string 20170103 is converted into a date based on the input format YYYYMMDD.

  •  YYYY: year in four digits format
  •  MM: month in  2 digits format
  •  DD: day in 2 digits format

As the result, the function returns January 3rd 2017.

The following table illustrates the template patterns for formatting date values:

PatternDescription
Y,YYYyear in 4 digits with comma
YYYYyear in 4 digits
YYYlast 3 digits of year
YYlast 2 digits of year
YThe last digit of year
IYYYISO 8601 week-numbering year (4 or more digits)
IYYLast 3 digits of ISO 8601 week-numbering year
IYLast 2 digits of ISO 8601 week-numbering year
ILast digit of ISO 8601 week-numbering year
BC, bc, AD or adEra indicator without periods
B.C., b.c., A.D. ora.d.Era indicator with periods
MONTHEnglish month name in uppercase
MonthFull capitalized English month name
monthFull lowercase English month name
MONAbbreviated uppercase month name e.g., JAN, FEB, etc.
MonAbbreviated capitalized month name e.g, Jan, Feb,  etc.
monAbbreviated lowercase month name e.g., jan, feb, etc.
MMmonth number from 01 to 12
DAYFull uppercase day name
DayFull capitalized day name
dayFull lowercase day name
DYAbbreviated uppercase day name
DyAbbreviated capitalized day name
dyAbbreviated lowercase day name
DDDDay of year (001-366)
IDDDDay of ISO 8601 week-numbering year (001-371; day 1 of the year is Monday of the first ISO week)
DDDay of month (01-31)
DDay of the week, Sunday (1) to Saturday (7)
IDISO 8601 day of the week, Monday (1) to Sunday (7)
WWeek of month (1-5) (the first week starts on the first day of the month)
WWWeek number of year (1-53) (the first week starts on the first day of the year)
IWWeek number of ISO 8601 week-numbering year (01-53; the first Thursday of the year is in week 1)
CCCentury e.g, 21, 22, etc.
JJulian Day (integer days since November 24, 4714 BC at midnight UTC)
RMMonth in uppercase Roman numerals (I-XII; I=January)
rmMonth in lowercase Roman numerals (i-xii; i=January)

PostgreSQL to_date examples

The following statement converts the string 10 Feb 2017 into a date value:

1
SELECT to_date('10 Feb 2017', 'DD Mon YYYY');

The output is:

1
2
3
4
  to_date
------------
2017-02-10
(1 row)

Suppose you want to convert the string 2017 Feb 10 to a date value, you can apply the pattern YYYY Mon DD as follows:

1
SELECT to_date('2017 Feb 20','YYYY Mon DD');

The function returns the following output:

1
2
3
4
  to_date
------------
2017-02-20
(1 row)

PostgreSQL to_date gotchas

If you pass an invalid date string, the to_date function will try to convert it to a valid date which you may not wanted. See the following example:

1
SELECT to_date('2017/02/30', 'YYYY/MM/DD');

It returns the following output:

1
2
3
4
  to_date
------------
2017-03-02
(1 row)

To convert this string to a date, you should cast the string literal to date explicitly:

1
SELECT '2017/02/30'::date;

PostgreSQL issued the following error:

1
2
ERROR:  date/time field value out of range: "2017/02/30"
LINE 1: SELECT '2017/02/30'::date;

In this tutorial, you have learned how to use the PostgreSQL to_date function to convert a string literal to a date value.

Previous Tutorial: PostgreSQL DATE_PART Function
Next Tutorial: PostgreSQL NOW Function

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Aggregate Functions

  • PostgreSQL COUNT Function
  • PostgreSQL AVG Function
  • PostgreSQL MAX Function
  • PostgreSQL MIN Function
  • PostgreSQL SUM Function

PostgreSQL Conditional Expressions

  • PostgreSQL CASE
  • PostgreSQL NULLIF
  • PostgreSQL COALESCE

PostgreSQL String functions

  • PostgreSQL CONCAT Function
  • PostgreSQL TRIM Function
  • PostgreSQL LENGTH Function
  • PostgreSQL REPLACE
  • PostgreSQL Substring
  • PostgreSQL Letter Case Functions

PostgreSQL Operators

  • PostgreSQL CAST

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • How To Change The Password of a PostgreSQL User
  • PostgreSQL AGE Function
  • PostgreSQL DATE_PART Function
  • PostgreSQL List Users
  • PostgreSQL NOW Function
  • PostgreSQL DATE_TRUNC Function
  • PostgreSQL TO_DATE Function: Convert String to Date
  • A Look at PostgreSQL User-defined Data Types
  • PostgreSQL Copy Database Made Easy
  • How to Get Table, Database, Indexes, Tablespace, and Value Size in PostgreSQL

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.