25

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
  • 704,970
  • 168
  • 1,303
  • 1,425
TheQuant
  • 273
  • 1
  • 3
  • 6

1 Answers1

33

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
  • 128,762
  • 24
  • 200
  • 224
  • 7
    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