isnull vs is null

后端 未结 10 1345
闹比i
闹比i 2020-12-06 00:13

I have noticed a number of queries at work and on SO are using limitations in the form:

isnull(name,\'\') <> \'\'

Is there a particul

10条回答
  •  春和景丽
    2020-12-06 00:37

    They don't mean the same thing.

    name is not null 
    

    This checks for records where the name field is null

    isnull(name,'') <> name  
    

    This one changes the value of null fields to the empty string so they can be used in a comparision. In SQL Server (but not in Oracle I think), if a value is null and it is used to compare equlaity or inequality it will not be considered becasue null means I don't know the value and thus is not an actual value. So if you want to make sure the null records are considered when doing the comparision, you need ISNULL or COALESCE(which is the ASCII STANDARD term to use as ISNULL doen't work in all databases).

    What you should be looking at is the differnece between

    isnull(a.name,'') <> b.name  
    

    a.name <> b.name

    then you will understand why the ISNULL is needed to get correct results.

提交回复
热议问题