function to convert ticks to DatetimeOffset

--function to convert ticks to DatetimeOffset
if  object_id(N'GetTimeFromTicks', N'FN') is null
begin
execute dbo.sp_executesql @statement = N'
CREATE FUNCTION dbo.GetTimeFromTicks(@Ticks BIGINT)
RETURNS DATETIME
AS
BEGIN
DECLARE @Days BIGINT
DECLARE @DaysBefore1753 BIGINT
DECLARE @TimeTicks BIGINT
DECLARE @Seconds BIGINT
 
SET @Days = @Ticks / CONVERT(BIGINT,864000000000)
SET @DaysBefore1753 = CONVERT(BIGINT,639905)
SET @TimeTicks = @Ticks % CONVERT(BIGINT,864000000000)
SET @Seconds = @TimeTicks / CONVERT(BIGINT,10000000)
 
RETURN ToDateTimeOffSet(DATEADD(s,@Seconds,DATEADD(d,@Days - @DaysBefore1753,CONVERT(DATETIME,''1/1/1753''))), ''+00:00'')
END'
 
end

 

你可能感兴趣的:(function)