Jump to content

varchar to timestamp


The Little Guy

Recommended Posts

I currently have a varchar for my timestamp, I haven't changed it to a timestamp field because I wasn't sure if it would mess up my dates, the dates are in a perfect timestamp format though.

 

So, if I update the datatype of that field will I get an error, or worse, and invalid date?

Link to comment
https://forums.phpfreaks.com/topic/245138-varchar-to-timestamp/
Share on other sites

I think it will work, but try it on a small test table.

CREATE TABLE test (a VARCHAR(100));
INSERT INTO test VALUES ("2011-08-18 12:34:56");

ALTER TABLE test MODIFY COLUMN a TIMESTAMP;

SELECT * FROM test;

 

If it doesn't work then

ALTER TABLE test ADD COLUMN b TIMESTAMP AFTER a;
UPDATE test SET b = a; -- or whatever transformation is necessary
ALTER TABLE test DROP COLUMN a;
ALTER TABLE test CHANGE COLUMN b a TIMESTAMP;

 

No promises on the exact SQL, I get mixed up between MySQL and MS SQL sometimes.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.