sql.ToTicks(<2016)

PHOTO EMBED

Wed Dec 14 2022 20:00:37 GMT+0000 (Coordinated Universal Time)

Saved by @rick_m #sql

CREATE FUNCTION dbo.ToTicks ( @DateTime datetime2 )
  RETURNS bigint
AS
BEGIN
    DECLARE @Days bigint = DATEDIFF( DAY, '00010101', cast( @DateTime as date ) );
    DECLARE @Seconds bigint = DATEDIFF( SECOND, '00:00', cast( @DateTime as time( 7 ) ) );
    DECLARE @Nanoseconds bigint = DATEPART( NANOSECOND, @DateTime );
    RETURN  @Days * 864000000000 + @Seconds * 10000000 + @Nanoseconds / 100;
END
content_copyCOPY

https://michaeljswart.com/2017/07/converting-from-datetime-to-ticks-using-sql-server/