Thursday 19 September 2019

T-SQL Queries for Epoch & Unix Timestamp Conversion

/* USE below script to Convert epoch to human-readable date and vice versa*/ 
--GET current Unix epoch time  
SELECT Datediff(second, '1970-01-01', Getdate()) AS [Current Unix Epoch Time]
go 
--Convert epoch to human-readable date  
DECLARE @UNIXEPOCHTIME BIGINT =Datediff(second, '1970-01-01', Getdate())
SELECT Dateadd(second, CASE 
                         WHEN Len(@UNIXEPOCHTIME) = 13 THEN 
                         @UNIXEPOCHTIME / 1000 
                         ELSE @UNIXEPOCHTIME 
                       END, '1970-01-01') AS CURRENTTIME 
/* Below is the output of the above query */

/* Thank you,
   Narasimha
*/

No comments:

Post a Comment