suppose
isnull(some_column, getdate()) >= getdate()
where logic is if some_column is null this expression should always be true. However will this always be so (since between two evaluations of getdate() some time has passed and they won’t be equal) ?
No, is not safe. You are facing so called runtime constants expressions, of which
GETDATE()is the bookcase example, which are evaluate once at query startup and the subsequently the cached evaluated value is used. However each occurence is evaluated separately once and the two evaluation can fall on the separate sides of the datetime precision boundary, resulting in two different values.A simple test reveals how this happens:
In my case this was hit right away:
I’m not addressing the question how to better do this (you already got plenty of advice) but the underlying question whether your assumption about the run-time execution is right (is not):
GETDATE()twice in a statement will be evaluate twice