Select rows where column is null

前端 未结 7 2084
余生分开走
余生分开走 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:10

    I'm not sure if this answers your question, but using the IS NULL construct, you can test whether any given scalar expression is NULL:

    SELECT * FROM customers WHERE first_name IS NULL
    

    On MS SQL Server, the ISNULL() function returns the first argument if it's not NULL, otherwise it returns the second. You can effectively use this to make sure a query always yields a value instead of NULL, e.g.:

    SELECT ISNULL(column1, 'No value found') FROM mytable WHERE column2 = 23
    

    Other DBMSes have similar functionality available.

    If you want to know whether a column can be null (i.e., is defined to be nullable), without querying for actual data, you should look into information_schema.

提交回复
热议问题