How to cast varchar into timestamp in postgres

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • krithikas
    New Member
    • Dec 2006
    • 4

    How to cast varchar into timestamp in postgres

    I have a table (which i cannot modify) where date and time fields are stored as VARCHAR. But i have to cast these fields into timestamp.

    My requirement is like,
    timestamp (select date, time where max(timestamp(d ate,time));

    How to do this in postgres?
  • michaelb
    Recognized Expert Contributor
    • Nov 2006
    • 534

    #2
    I'm not sure if you can really cast varchar into timestamp, (although I don't have the latest version of Postgres, which may have a larger set of cast operators), but you can try something like this instead:

    Code:
    select (mydate || ' ' || mytime) from foo where (mydate || ' ' || mytime) = (select max (mydate || ' ' || mytime) from foo);

    Comment

    • krithikas
      New Member
      • Dec 2006
      • 4

      #3
      Thank you,
      I found a function to_timestamp and it casts string into timestamp
      I tried sth like ,

      to_timestamp('0 1 Jan 2007 09:09:09', 'DD Mon YYYY HH:MI:SS')

      It works .

      Comment

      • michaelb
        Recognized Expert Contributor
        • Nov 2006
        • 534

        #4
        Great, thanks for letting me know.
        michael

        Comment

        Working...