SQL Server中的NULL条件[重复]

时间:2021-11-02 10:19:36

This question already has an answer here:

这个问题在这里已有答案:

I have a table like this:

我有这样一张桌子:

ID      | Code    | YEAR
--------+---------+----------
0       | 1       | '1998'
1       | 5       |  NULL
2       | 7       | '2013'
3       | 1       | '1892'
4       | 5       |  NULL
5       | 7       | '1900'

I have a combobox with 3 values: All, NULLs, Not Nulls.

我有一个有3个值的组合框:All,NULLs,Not Nulls。

ALL: load All rows and no condition.

ALL:加载所有行,没有条件。

SELECT * FROM tbl_Location

Nulls:

SELECT * FROM tbl_Location Where YEAR is Null

'Not Nulls'

SELECT * FROM tbl_Location Where YEAR is not Null

'All' is a combo-box value that load all rows without condition

'All'是一个组合框值,可以无条件地加载所有行

I want to do this all in one query. What can I do?

我想在一个查询中完成所有这些操作。我能做什么?

1 个解决方案

#1


2  

You need to pass the value of the Combobox in @Status:

你需要在@Status中传递Combobox的值:

Here you need to set conditions like this:

在这里你需要设置这样的条件:

DECLARE @Status varchar(15)
--set the Status

SELECT * 
FROM tbl_Location
WHERE (@Status = 'All' 
       OR (@Status = 'Nulls' AND YEAR IS NULL) 
       OR (@Status = 'Not Nulls' AND YEAR IS NOT NULL)
      )

#1


2  

You need to pass the value of the Combobox in @Status:

你需要在@Status中传递Combobox的值:

Here you need to set conditions like this:

在这里你需要设置这样的条件:

DECLARE @Status varchar(15)
--set the Status

SELECT * 
FROM tbl_Location
WHERE (@Status = 'All' 
       OR (@Status = 'Nulls' AND YEAR IS NULL) 
       OR (@Status = 'Not Nulls' AND YEAR IS NOT NULL)
      )