Sql-server – Convert a datetime column to number of seconds

datetimesql serversql-server-2008t-sqltype conversion

In my SQL Server database, I have a datetime column.

What is a good way to create a new column that represents the long value for the datetime column? The long would represent a number of seconds.

I thought if I can convert it to longs, it would make it easier to do group by queries over time periods, as I could just divide the long number by fixed amounts.

The table is static, Won't be updating or deleting data.

Best Answer

Create a new column (ALTER TABLE) then run an UPDATE on it

UPDATE
  MyTable
SET
  NewIntColumn = DATEDIFF(SECOND, '19000101', MyDateTimeColumn)

19000101 is the SQL Server epoch. You can use 19700101 for Unix epoch for example