Created
October 30, 2017 11:21
-
-
Save bhrgu/41964aaa1293e237631331c04efc8751 to your computer and use it in GitHub Desktop.
T-SQL UNIX timestamp function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Create function */ | |
CREATE FUNCTION [dbo].[fn_GetUnixTimestamp] | |
( | |
@CT DATETIME | |
) | |
RETURNS BIGINT | |
AS | |
BEGIN | |
DECLARE @CTS BIGINT | |
SELECT @CTS = (CAST(DATEDIFF(second, '1970-01-01', CAST(GetUtcDate() AS date)) AS bigint)*1000) | |
+ DATEDIFF(ms, CAST(GetUtcDate() AS date), GetUtcDate()) | |
RETURN @CTS | |
END | |
/* Usage */ | |
SELECT [dbo].[fn_GetUnixTimestamp](GetUtcDate()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment