Jump to content

Change date stored as epoch to datetime when altering table?


Go to solution Solved by requinix,

Recommended Posts

I am changing the name of one of the fields in my table:

 

ALTER TABLE `users` CHANGE `user_date` `created_at` DATETIME NOT NULL;

 

The old field, "user_date" is int(10) and storing time in epoch format. The new field is just a datetime field. Is it possible to modify this query so that the time is converted to datetime?

 

Thanks.

  • Solution

I suspect doing that will try to interpret existing data as YYYYMMDDHHMMSS strings, not as Unix timestamps.

 

Will definitely work: add the created_at column, UPDATE it using FROM_UNIXTIME(user_date), then drop user_date. Three statements.

  • Like 1

I suspect doing that will try to interpret existing data as YYYYMMDDHHMMSS strings, not as Unix timestamps.

 

Will definitely work: add the created_at column, UPDATE it using FROM_UNIXTIME(user_date), then drop user_date. Three statements.

 

Yep, this is what I ended up doing. I was just curious if there might have been a one liner way.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.