It is preferable to store the values in the right datatype i.e., datetime or datetime2 (as suggested by @Dale K.
I would suggest you to follow below steps.
- Create new table with just two columns: PrimaryKeycolumn, NewDateTime column
CREATE TABLE data_copy(PKColumn INT, NewDatetimeColumn DATETIME2);
- Load data into data_copy from original data table
INSERT INTO data_copy(PKColumn, NewDatetimeColumn)
SELECT PKColumn, DATEADD(second, [datetime] / 1000, '1970-01-01')
FROM [database].[dbo].[data];
- Now, Update the datetime column to NULL values
UPDATE [database].[dbo].[data]
SET [datetime]= NULL;
- Now, Alter the datatype to DATETIME
--IF not null set accordingly
ALTER TABLE [database].[dbo].[data] ALTER COLUMN [datetime] DATETIME2 NULL;
- Now, Update the datetime values from the copy table
UPDATE d
SET d.[datetime] = c.datetimecolumn
FROM [database].[dbo].[data] AS d
JOIN [database].[dbo].[data_copy] AS c
ON d.PKColumn = c.PKColumn