snippetsqlModerate
Convert a datetime column to number of seconds
Viewed 0 times
numbercolumnconvertsecondsdatetime
Problem
In my SQL Server database, I have a
What is a good way to create a new column that represents the
I thought if I can convert it to
The table is static, Won't be updating or deleting data.
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.
Solution
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 exampleCode Snippets
UPDATE
MyTable
SET
NewIntColumn = DATEDIFF(SECOND, '19000101', MyDateTimeColumn)Context
StackExchange Database Administrators Q#18042, answer score: 13
Revisions (0)
No revisions yet.