Depending on what he thinks will work best.
Now he is functionally lazy, which is important. For example. if col1 is a varchar that will always contain a number, when col2 is null, then
isnull(col2, cast(col1 as int))
Will work.
However, he did not indicate whether he would try listing before or simultaneously with a null check and there is an error if col2 not null, or if he will try to perform only listing if col2 null.
At least we expect it to get col1 anyway, because one scan of a table receiving 2 values ββwill be faster than two scans receiving one each.
The same SQL commands can be executed in different ways, because the instructions we give turn into lower-level operations based on knowledge of indexes and statistics about tables.
For this reason, in terms of performance, the answer is "when it seems that it would be a good idea, otherwise it is not."
In terms of observable behavior, he is lazy.
Edit: Michael Ericsson's answer shows that there are cases that can really make a mistake due to the fact that they are not lazy. I will adhere to my answer here in terms of impact on performance, but it is vital in terms of correct impact, at least in some cases.
Jon hanna
source share