Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Current Unix Epoch Time-stamp in seconds in SQL Server

Tags:

sql

sql-server

I have the following C# method to get current unix epoch time stamp,

    public static long GetCurrentUnixTimestampSeconds()
    {
        var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        return (long)(DateTime.UtcNow - unixEpoch).TotalSeconds;
    }

I need the same in SQL Server. Is it possible?

like image 611
Imran Qadir Baksh - Baloch Avatar asked Sep 17 '14 17:09

Imran Qadir Baksh - Baloch


1 Answers

This is just the number of seconds between epoch and current UTC time:

SELECT DATEDIFF(SECOND, '19700101', sysutcdatetime());
like image 124
Aaron Bertrand Avatar answered Oct 12 '22 23:10

Aaron Bertrand