Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert date to datetime or timestamp using alter table in mysql

Tags:

mysql

I have a table in mysql with DATE column, now I need to convert these values to DATETIME or TIMESTAMP.

I wonder what will happen with data when I'll do:

ALTER TABLE sample MODIFY sample_date DATETIME

or

ALTER TABLE sample MODIFY sample_date TIMESTAMP
like image 245
aeryaguzov Avatar asked Dec 09 '15 11:12

aeryaguzov


People also ask

How do I change data from date to TIMESTAMP?

Insert Date and Timestamp Using NOW FunctionRight-click on the cell and select 'Format cells'. In the Format Cells dialog box, select 'Custom' category in the Number tab. In the Type field, enter dd-mm-yyyy hh:mm:ss. Click OK.

How do I add a TIMESTAMP to a MySQL table?

Here is the SQL you can use to add the column in: ALTER TABLE `table1` ADD `lastUpdated` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ; This adds a column called 'lastUpdated' with a default value of the current date/time.


1 Answers

It's pretty straight forward to convert from one type to another using the ALTER TABLE command:

ALTER TABLE table_name CHANGE old_date new_timestamp TIMESTAMP This process may take a while to complete if you have a lot of data and a large number of indexes.

I'm not sure why you'd want to switch these to the TIMESTAMP type as that has a much more limited range than DATETIME. The differences are documented and important to know.

like image 94
Kanike Vamshi Krishna Avatar answered Sep 22 '22 03:09

Kanike Vamshi Krishna