Is Sql Server's ISNULL() function lazy/short-circuited?

TIs ISNULL() a lazy function?

That is, if i code something like the following:

  • When would you use GUIDs as primary keys?
  • sql server backup not truncating transaction log
  • sql datetime column - insert time as 00:00:00.000
  • Creating an SQL Server Script to check for nulls on any table given in a stored procedure
  • SQL server 2008 backup error - Operating system error 5(failed to retrieve text for this error. Reason: 15105)
  • Invalid length parameter error using SUBSTRING - SQL Server 2008
  • SELECT ISNULL(MYFIELD, getMyFunction()) FROM MYTABLE

    will it always evaluate getMyFunction() or will it only evaluate it in the case where MYFIELD is actually null?

  • How to make ISNULL not just return true or false
  • What is the function of the following SQL ISNULL statement
  • how to apply isnull to variable?
  • Sargable queries using ISNULL in TSQL
  • Comparing with null by converting value to a constant with isnull
  • ISNULL() returning NULL
  • 4 Solutions collect form web for “Is Sql Server's ISNULL() function lazy/short-circuited?”

    It’s whichever it thinks will work best.

    Now it’s functionally lazy, which is the important thing. E.g. if col1 is a varchar which will always contain a number when col2 is null, then

    isnull(col2, cast(col1 as int))
    

    Will work.

    However, it’s not specified whether it will try the cast before or simultaneously with the null-check and eat the error if col2 isn’t null, or if it will only try the cast at all if col2 is null.

    At the very least, we would expect it to obtain col1 in any case because a single scan of a table obtaining 2 values is going to be faster than two scans obtaining one each.

    The same SQL commands can be executed in very different ways, because the instructions we give are turned into lower-level operations based on knowledge of the indices and statistics about the tables.

    For that reason, in terms of performance, the answer is “when it seems like it would be a good idea it is, otherwise it isn’t”.

    In terms of observed behaviour, it is lazy.

    Edit: Mikael Eriksson’s answer shows that there are cases that may indeed error due to not being lazy. I’ll stick by my answer here in terms of the performance impact, but his is vital in terms of correctness impact across at least some cases.

    This works fine

    declare @X int
    set @X = 1
    select isnull(@X, 1/0)
    

    But introducing an aggregate will make it fail and proving that the second argument could be evaluated before the first, sometimes.

    declare @X int
    set @X = 1
    select isnull(@X, min(1/0))
    

    Judging from the different behavior of

    SELECT ISNULL(1, 1/0)
    
    SELECT ISNULL(NULL, 1/0)
    

    the first SELECT returns 1, the second raises a Msg 8134, Level 16, State 1, Line 4
    Divide by zero error encountered.
    error.

    This “lazy” feature you are referring to is in fact called “short-circuiting”
    And it does NOT always work especially if you have a udf in the ISNULL expression.
    Check this article where tests were run to prove it:
    Short-circuiting (mainly in VB.Net and SQL Server)

    T-SQL is a declarative language hence it cannot control the algorithm used to get the results.. it just declares what results it needs. It is upto the query engine/optimizer to figure out the cost-effective plan. And in SQL Server, the optimizer uses “contradiction detection” which will never guarantee a left-to-right evaluation as you would assume in procedural languages.


    For your example, did a quick test:
    Created the scalar-valued UDF to invoke the Divide by zero error:

    CREATE FUNCTION getMyFunction
    ( @MyValue INT )
    RETURNS INT
    AS
    BEGIN
        RETURN (1/0)
    END
    GO
    

    Running the below query did not give me a Divide by zero error encountered error.

    DECLARE @test INT
    SET @test = 1
    SET @test = ISNULL(@test, (dbo.getMyFunction(1)))
    SELECT @test
    

    Changing the SET to the below statement did give me the Divide by zero error encountered. error. (introduced a SELECT in ISNULL)

    SET @test = ISNULL(@test, (SELECT dbo.getMyFunction(1)))
    

    But with values instead of variables, it never gave me the error.

    SELECT ISNULL(1, (dbo.getMyFunction(1)))
    SELECT ISNULL(1, (SELECT dbo.getMyFunction(1)))
    

    So unless you really figure out how the optimizer is evaluating these expressions for all permutations, it would be safe to not rely on the short-circuit capabilities of T-SQL.

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.