Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change a mySQL Column datatype from text to timestamp

Tags:

mysql

How can I change the datatype of a column from text to timestamp. The current text column is storing dates in the format : '2010-08-15' (yyyy-mm-dd sql format)

like image 408
Deepank Gupta Avatar asked Jan 06 '11 05:01

Deepank Gupta


People also ask

What is the datatype for timestamp in MySQL?

MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.


2 Answers

ALTER TABLE `mydb`.`mytable` MODIFY COLUMN `mycol` TIMESTAMP;

Using the above command a value such as:

'2010-08-15'

will change to

TIMESTAMP '2010-08-15 00:00:00'

Link to documentation.

like image 144
mechanical_meat Avatar answered Sep 28 '22 02:09

mechanical_meat


Have you tried this

alter table table_name change field_name field_name timestamp;

where table_name is the name of the table and field_name is the name of the field that you wanted alter the type of.

like image 22
Nishant Avatar answered Sep 28 '22 01:09

Nishant