I am trying to get Datediff
between GETDATE()
and SYSDATETIME()
in milliseconds.
SELECT DATEDIFF(ms, GETDATE() , SYSDA
They differ because the two functions can't be called simultaneously (at the exact same time). Other processes running can affect the timings. There are dozens of reasons they can differ by varying amounts.
If you do the same thing with two calls to GetDate()
instead, they result in no difference, because the database engine is smart enough to figure out they're the same thing and re-use the results. Using GetDate()
and SysDateTime()
is different, though, because they're not the same code path (they do different things).
Think of it this way: If you see 1 + 2
and 1 + 2
, it's easy to see that the first expression and the second are the same, and so you only have to do the calculation once. If you change it to 1 + Rand()
and 1 + Rand()
, you have no way of knowing what the two different calls to Rand()
will return, so you have to do the calculations separately.