• I had also forgotten to adjust the seconds. When the earlier date has a larger fractional part than the later date, as well as adding the fractional part to 1000000000, I needed to be subtracting 1 from s_difference, the difference in seconds. I changed the query again so that the CROSS APPLY to calculate tt is as follows:

    CROSS APPLY (SELECT tt = case when nsfraction < 0 then DATEADD(second, s_difference - 1, 0)

    else DATEADD(second, s_difference + 1, 0)

    end) d

    One more thing about the original code, it takes into account the case where the timespan is greater than a day, which is very nice, since, as much as my client assures me that will never happen, I've been assured of many things in my time. This code will not break if that eventuality ever comes about.