Select rows where column is null

前端 未结 7 2103
余生分开走
余生分开走 2021-02-03 18:49

How do you write a SELECT statement that only returns rows where the value for a certain column is null?

7条回答
  •  忘了有多久
    2021-02-03 19:11

    You want to know if the column is null

    select * from foo where bar is null
    

    If you want to check for some value not equal to something and the column also contains null values you will not get the columns with null in it

    does not work:

    select * from foo where bar <> 'value'
    

    does work:

    select * from foo where bar <> 'value' or bar is null
    

    in Oracle (don't know on other DBMS) some people use this

    select * from foo where NVL(bar,'n/a') <> 'value'
    

    if I read the answer from tdammers correctly then in MS SQL Server this is like that

    select * from foo where ISNULL(bar,'n/a') <> 'value'
    

    in my opinion it is a bit of a hack and the moment 'value' becomes a variable the statement tends to become buggy if the variable contains 'n/a'.

提交回复
热议问题