Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Query - how do filter by null or not null

I want to filter a record....

If statusid is null, filter the record (where statusId is not null)

If statusid is not null, filter the record where statusid is equal to the specified statusid.

How do I do this?

like image 689
001 Avatar asked Mar 28 '11 03:03

001


People also ask

How do I filter NULL value?

Filtering on your formula After creating the formula for what you want to do, you can filter on the formula column you created in the search bar by typing the value {blank}, {empty}, or {null}, which will act as a filter. Then, you can type normal filter syntax, such as customer name = {empty} or department != {null} .

Is NULL SQL filter?

SQL NULL Filtering for NULL in queries NULL ) will always yield the truth value of UNKNOWN which will be rejected by WHERE . WHERE filters all rows that the condition is FALSE or UKNOWN and keeps only rows that the condition is TRUE .

How do I SELECT data without NULL values in SQL?

Below is the syntax to filter the rows without a null value in a specified column. Syntax: SELECT * FROM <table_name> WHERE <column_name> IS NOT NULL; Example: SELECT * FROM demo_orders WHERE ORDER_DATE IS NOT NULL; --Will output the rows consisting of non null order_date values.


2 Answers

Just like you said

select * from tbl where statusid is null 

or

select * from tbl where statusid is not null 

If your statusid is not null, then it will be selected just fine when you have an actual value, no need for any "if" logic if that is what you were thinking

select * from tbl where statusid = 123 -- the record(s) returned will not have null statusid 

if you want to select where it is null or a value, try

select * from tbl where statusid = 123 or statusid is null 
like image 56
JeremyWeir Avatar answered Sep 22 '22 17:09

JeremyWeir


How about statusid = statusid. Null is never equal to null.

like image 22
Zachary Scott Avatar answered Sep 22 '22 17:09

Zachary Scott