EDIT: Casting to float / int no longer works in recent versions of SQL Server. Use the following instead:
select datediff(day, '1899-12-30T00:00:00', my_date_field) from mytable
Please note that the string date must be in an explicit date format so as not to affect the regional settings of your server.
In older versions of SQL Server, you can convert from DateTime to Integer by casting to float and then to int:
select cast(cast(my_date_field as float) as int) from mytable
(NB: you cannot use directly in int since MSSQL rounds the value up if you are in the middle of the day!)
If there is an offset in your data, you can obviously add or subtract this from the result
You can convert in the other direction, swinging straight back:
select cast(my_integer_date as datetime) from mytable
Steve mayne
source share