Skip to content

Instantly share code, notes, and snippets.

@bhrgu
Created October 30, 2017 11:21
Show Gist options
  • Save bhrgu/41964aaa1293e237631331c04efc8751 to your computer and use it in GitHub Desktop.
Save bhrgu/41964aaa1293e237631331c04efc8751 to your computer and use it in GitHub Desktop.
T-SQL UNIX timestamp function
/* 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