TheQuant
TheQuant

Reputation: 313

Convert datetime column to datetime2 column in SQL Server?

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

Upvotes: 29

Views: 28341

Answers (1)

SQLMenace
SQLMenace

Reputation: 134941

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

Upvotes: 40

Related Questions