Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Column from Date to Datetime

I have a column named Lastmodified, with a data type of Date, but it should have been DateTime.

Is there any way of converting the column?

When I use the 'Design' feature of SQL Server Management Studio I get the following error:

Saving changes is not permitted. The changes you have made require the following table to be dropped and re-created.

Not really interested in dropping the table, I just want to know if it is possible to convert a column from Date to Datetime or do I have to delete the column and create a new one with the correct data type?

like image 438
gulbaek Avatar asked Dec 08 '11 11:12

gulbaek


People also ask

How do I convert a column to a time in Python?

Use pandas to_datetime() function to convert the column to DateTime on DataFrame. Use the format parameter of this method to specify the pattern of the DateTime string you wanted to convert.


2 Answers

It shouldn't need to drop the table and recreate it, unless that column is taking part in one or more constraints.

You can just do it using SQL:

ALTER TABLE Tab ALTER COLUMN LastModified datetime2 not null

(I chose datetime2 over datetime, since the former is recommended for all new development work, and since the column is currently date, I know you're on SQL Server 2008 or later)

like image 186
Damien_The_Unbeliever Avatar answered Sep 17 '22 15:09

Damien_The_Unbeliever


That's just a safety setting in SQL Server Mgmt Studio - you can turn it off, if you're adventurous :-)

enter image description here

Disable the checkbox there and you can do whatever you like!

like image 45
marc_s Avatar answered Sep 19 '22 15:09

marc_s