I need to convert a DateTime type value to BIGINT type in .Net ticks format (number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001).<
This function returns the unix time:
alter FUNCTION [dbo].[GETTICKS] (@datetime datetime)
RETURNS bigint
WITH SCHEMABINDING
AS
BEGIN
RETURN
DATEPART(millisecond,@datetime) +
CONVERT(bigint,
1000 * (
DATEPART(second,@datetime) +
60 * DATEPART(minute,@datetime) +
3600 * DATEPART(hour,@datetime) +
3600 * 24 * DATEPART(DAYOFYEAR,@datetime) +
convert(bigint, 3600 * 24 * (((DATEPART(year,@datetime) - 1970) * 365) + ((DATEPART(year,@datetime) - 1972) / 4)))
) )
END