Question : to  optimise the case statement

could you optimise below case statement

case
        when (u.bi_user_passport_puid is not null
              AND u.bi_owner_passport_puid is not null
              AND u.bi_user_passport_puid = u.bi_owner_passport_puid) then
                1
                else
                0
                end                            AS IsFamilyHead

thanks in advance

Answer : to  optimise the case statement

When the setting ANSI_NULLS is set to ON (SQL Server default), the NULL-check is redundant because NULL <> NULL and [any value] <> NULL.

Thus, only "u.bi_user_passport_puid = u.bi_owner_passport_puid" would suffice. When one or both are NULL, the outcome will always be negative.

You can check the setting with the command:
DBCC USEROPTIONS
If the set option "ansi_nulls" is in the list, it's ON.
1:
2:
3:
4:
CASE
   WHEN u.bi_user_passport_puid = u.bi_owner_passport_puid) THEN 1
   ELSE 0
END AS IsFamilyHead
Random Solutions  
 
programming4us programming4us