How to get the epoch time in SQL Server?

freeman picture freeman · Apr 1, 2012 · Viewed 33k times · Source

The scenario is this: select max date from some table, when the target table having no data, so the max date is null. when the date being null, I want to get the earliest date of system, so the epoch time seems perfect.

I have searched some ways including DATEADD functions, but that seems not elegant.

Answer

Mitch Wheat picture Mitch Wheat · Apr 1, 2012

If I understand your question correctly, in SQL Server the epoch is given by cast(0 as datetime) :

select Max(ISNULL(MyDateCol, cast(0 as datetime)))
from someTable
group by SomeCol