Cast a String to Timestamp in PostgreSQL

Posted August 20th, 2008 by Chris

If storing a date/timestamp in a varchar or text field in postgres, the to_timestamp() function will let you access the value as a ‘real’ date. The second parameter tells the function the format of your string:

SELECT to_timestamp('2008-08-01', 'YYYY-MM-DD') AS date;

Tags: , , ,

This entry was posted on Wednesday, August 20th, 2008 at 11:48 am and is filed under Personal, Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Responses to “Cast a String to Timestamp in PostgreSQL”

  1. Sam Bishop says:

    Just a note to anyone else sent here by Google… The code snippet is missing the underscore between “to” and “timestamp”.

    Thanks, Chris!

  2. Chris says:

    Thanks Sam,

    Looks like the code parser I used was stripping the underscore, so I’ve taken out the code highlighting to show the correct syntax.

    Chris

Post a Comment