如何在 SQL 中过滤掉包含具有空数据或空数据的特定列的行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/325370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 00:22:58  来源:igfitidea点击:

How can I filter out the rows which contain a particular column with null or empty data in SQL?

sql

提问by Nakul Chaudhary

In SQL, How we make a check to filter all row which contain a column data is null or empty ?
For examile

在 SQL 中,我们如何检查以过滤包含列数据为空或空的所有行?
例如

Select Name,Age from MEMBERS

We need a check Name should not equal to null or empty.

我们需要检查 Name 不应等于 null 或空。

回答by Vinko Vrsalovic

This will work in all sane databases (wink, wink) and will return the rows for which name is not null nor empty

这将适用于所有健全的数据库(wink,wink),并将返回名称不为空也不为空的行

select name,age from members where name is not null and name <> ''

回答by Tony Andrews

For DBMSs that treat '' as a value (not null), Vinko's query works:

对于将 '' 视为值(非空)的 DBMS,Vinko 的查询有效:

select name,age from members where name is not null and name <> ''

For Oracle, which treats '' as a null, tha above doesn't work but this does:

对于将 '' 视为空值的 Oracle,上面的 tha 不起作用,但这样做:

select name,age from members where name is not null

I tried to think of a "DBMS-agnostic" solution, but failed - mainly due to the different concatenation operators/functions used by different DBMSs!

我试图想出一个“与 DBMS 无关”的解决方案,但失败了 - 主要是由于不同 DBMS 使用的连接运算符/函数不同!

回答by Fuangwith S.

SELECT Name,Age FROM MEMBERS WHERE name IS NOT null OR name <> ''

SELECT Name,Age FROM MEMBERS WHERE name IS NOT null OR name <> ''

You can get more informations from http://www.w3schools.com/sql/default.asp

您可以从http://www.w3schools.com/sql/default.asp获取更多信息

回答by Dheer

Depending on the Database being used; try Select Name, Age from Members where name IS NOT NULL

取决于正在使用的数据库;尝试从姓名不为空的成员中选择姓名、年龄

if you need to filter the otherway Select Name, Age from Members where name IS NULL

如果您需要以其他方式过滤 Select Name, Age from Members where name IS NULL

Certain RDBMS treat enpty string different from null, and you would need to add; Select Name, Age from Members where Name IS NOT NULL OR Name <> ''

某些 RDBMS 处理与 null 不同的 enpty 字符串,您需要添加;从名称不为空或名称 <> '' 的成员中选择名称、年龄

回答by Gilles

nvl(Name, 'some dumb string') this will return Name if Name is not null and different of '' (oracle, don't know for others). It will be equal to 'some dumb string' if null or equal to ''.

nvl(Name, 'some dual string') 如果 Name 不为空且与 '' 不同,这将返回 Name (oracle,不知道其他人)。如果为 null 或等于 '',它将等于 '一些哑字符串'。

回答by edosoft

T-SQL

查询语句

select name,age from members where COALESCE(name, '') <> ''