Skip to content

TO_DATE

Return a DATE value from a character string.

TO_DATE(string, format)

The input string is the first argument, and its format is the second argument. You must specify the format of the input string. See Formats for Datetime Values.

For example:

premdb=# SELECT TO_DATE('30 May 2016', 'DD Mon YYYY');
  to_date   
------------
 2016-05-30
(1 row)
premdb=# SELECT TO_DATE('May 31 2016','Mon DD YYYY');
  to_date   
------------
 2016-05-31
(1 row)

The following example converts a string to a DATE, then inserts it into a TIMESTAMP column:

premdb=# INSERT INTO match(matchday) SELECT TO_DATE('May 31 2016','Mon DD YYYY');
INSERT 0 1
premdb=# SELECT * FROM match WHERE matchday='2016-05-31';
 seasonid |      matchday       | htid | atid | ftscore | htscore 
----------+---------------------+------+------+---------+---------
         | 2016-05-31 00:00:00 |      |      |         | 
(1 row)

Parent topic:Formatting Functions