UNIX_TIMESTAMP in SQL Server

TheZver picture TheZver · Jan 12, 2012 · Viewed 72.4k times · Source

I need to create a function in SQL Server 2008 that will mimic mysql's UNIX_TIMESTAMP().

Thanks in advance !

Answer

Dunc picture Dunc · Jan 13, 2016

If you're not bothered about dates before 1970, or millisecond precision, just do:

-- SQL Server
SELECT DATEDIFF(s, '1970-01-01 00:00:00', DateField)

Almost as simple as MySQL's built-in function:

-- MySQL
SELECT UNIX_TIMESTAMP(DateField);

Other languages (Oracle, PostgreSQL, etc): How to get the current epoch time in ...