29

I have a SQL Server 2005 database with a datetime column. There is already data in the table but now the customer needs dates before 1753. So I decided to migrate the database to a SQL Server 2008 to use the datetime2 type.

However I can't just switch the type of the column from datetime to datetime2. Is there a way to do this conversion or do I have to reimport the data?

Thank you,

Daniel

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
TheQuant
  • 313
  • 1
  • 3
  • 6

1 Answers1

39

However I can't just switch the type of the column from datetime to datetime

Sure you can, use ALTER TABLE TableNAme ALTER column ColumnNAme datetime2

example

USE tempdb
GO

CREATE TABLE Test(SomeDate DATETIME)
INSERT Test values (GETDATE())

SELECT * FROM Test
GO

ALTER TABLE Test ALTER column SomeDate datetime2
GO

INSERT Test values ('16000101')

SELECT * FROM Test
GO
SQLMenace
  • 132,095
  • 25
  • 206
  • 225
  • 9
    It's not as simple if your column has dependencies, such as indexes and constraints. There is no other way than dropping and re-creating those. – MarioDS Jun 09 '16 at 13:12
  • Having the same challenge here 11 years later. Going for a new column, double maintenance, and then deleting the old one. – Stephan Møller Aug 02 '22 at 07:32
  • @MarioDS On top of that, I may just now be learning that it doesn't actually *check* for those dependencies before changing it...? – mochsner Aug 05 '22 at 18:02