Well, I think I was able to implement this myself. Here is the function:
IF EXISTS
(
SELECT 1
FROM sysobjects
WHERE id = OBJECT_ID('[dbo].[fnDateTimeToFileTime]')
AND type = 'FN'
)
BEGIN
DROP FUNCTION [dbo].[fnDateTimeToFileTime]
END
GO
CREATE FUNCTION [dbo].[fnDateTimeToFileTime]
(
@DateTime AS DATETIME
)
RETURNS
BIGINT
BEGIN
IF @DateTime IS NULL
RETURN NULL
DECLARE @MsecBetween1601And1970 BIGINT
DECLARE @MsecBetween1970AndDate BIGINT
SET @MsecBetween1601And1970 = 11644473600000
SET @MsecBetween1970AndDate =
DATEDIFF(ss, CAST('1970-01-01 00:00:00' as DATETIME), @DateTime) *
CAST(1000 AS BIGINT)
RETURN (@MsecBetween1601And1970 + @MsecBetween1970AndDate) * CAST(10000 AS BIGINT)
END
GO
IF @@ERROR = 0
GRANT EXECUTE ON [dbo].[fnDateTimeToFileTime] TO Public
GO
It seems to be accurate to 1 second, which is good with me (I could not make it more accurate due to data overflow). I used the TimeAndDate web tool to calculate the duration between dates.
What do you think?
source
share