1

In a Postgres database I have a table that in one of its columns holds a date as a character varying data type. Its format is dd MMM yyyy HH:mm.

Eg.: 21 Sep 2012 12:23.

Is there a way to convert it in a timestamp fromat so that I can compare it with the current time (i.e. now()- function ) ?

1

3 Answers 3

4

There are SET DATA TYPE and USING parts to the ALTER TABLE statement. If you wanted to fix the column permanently.

ALTER TABLE foo
    ALTER COLUMN my_time_stamp SET DATA TYPE timestamp
    USING to_timestamp(my_time_stamp);

See docs

Sign up to request clarification or add additional context in comments.

1 Comment

I need it to stay a character varying data type. Anyway, as A.H. mentiond very good pointed.
2

You're looking for to_timestamp function:

SELECT to_timestamp('21 Sep 2012 12:23', 'DD Mon YYYY HH24:MI')

http://sqlfiddle.com/#!12/d41d8/785

2 Comments

I'm sure that you do not want to use HH but HH24.
@A.H. Thank you, I didn't even know HH12 is the default.
2

I had somewhere read about to_timestamp try this out to_timestamp(text, text)

functionname:---to_timestamp(text, text)    
returntype---timestamp with time zone 
its function---convert string to time stamp 

example: to_timestamp('05 Dec 2000', 'DD Mon YYYY')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.