How can I change VARCHAR()
type to DATETIME
using ALTER
in MySQL?
Asked
Active
Viewed 4.8k times
22

Chris Diver
- 19,362
- 4
- 47
- 58

aizaz
- 3,056
- 9
- 25
- 57
-
5Do you have existing data in your varchar column that you want to convert into datetime? – beny23 Mar 11 '13 at 10:16
-
Does you table column has data already ? – Pranav Mar 11 '13 at 10:18
-
@beny23, Pranav: what should I do If there is some data exist in table. – ABC Apr 27 '14 at 05:27
-
@beny23 Yes. It's an ISO8601 formatted date/time string. Will MySQL convert it correctly? – Michael Dec 21 '17 at 22:21
3 Answers
25
ALTER TABLE <tblName> MODIFY <columnName> dataType constraint;
For your requirement it will be
ALTER TABLE <tblName> MODIFY <columnName> datetime;
Refer http://dev.mysql.com/doc/refman/5.1/en/alter-table.html

Meherzad
- 8,433
- 1
- 30
- 40
2
Try this query.
ALTER TABLE `table_name` CHANGE `From Date` `From Date` DATETIME NULL DEFAULT '0000-00-00 00:00:00';

Dhinakar
- 4,061
- 6
- 36
- 68
-
1Zero dates are not valid in mysql 5.7+. Default to either `NULL` or `"1970-01-01 00:00:01"` (unix epoch, first valid date in strict mode). Other options are `DEFAULT NOW()` or `ON UPDATE CURRENT_TIMESTAMP` depending on need. Note that `DEFAULT NOW()` is not legacy compatible. – mopsyd Nov 12 '18 at 16:54
-1
Why not you just use
STR_TO_DATE(str,format)
,
It takes a string str and a format string format and returns a DATETIME value if the format string contains both date and time parts.
Reffer this LINK , Hope it may help you