0

I have a varying character field (datestring) with a timestamp and I want to convert it to timestamp type. I can successfully create a new timestamp column (see below) and populate this column with a formatted version of the column datestring but I would rather not create a new field, I just want to replace the datestring field with a timestamp version of itself.

So this works fine:

ALTER TABLE service_areas ADD COLUMN newdate timestamp
update service_areas set newdate = to_timestamp(datestring, 'YYYY-MM-DD HH24:MI:SS.MS')

But I'd like to do something like:

update service_areas set datestring = to_timestamp(datestring, 'YYYY-MM-DD HH24:MI:SS.MS')

Suggestions?

Z

1 Answer 1

1

Use a USING clause

postgres=# CREATE TABLE foo(a varchar);
CREATE TABLE
postgres=# INSERT INTO foo VALUES('2013-07-15');
INSERT 0 1
postgres=# ALTER TABLE foo ALTER COLUMN a TYPE date USING to_date(a,'YYYY-MM-DD');
ALTER TABLE
Sign up to request clarification or add additional context in comments.

1 Comment

Worked perfectly! (I am new to the site and tried to mark this as the correct answer, let me know if I did not do it correctly).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.