如何删除查询中的NULL
单元格,因为即使CompanyName
不存在,也会出现一些记录?
SELECT ClientID, CompanyName, FirstName, LastName, Street, City
FROM Client
WHERE CompanyName LIKE '%G%' Or FirstName LIKE '%J%' OR LastName LIKE '%J%'
OR Street LIKE '%J%' OR City LIKE '%J%' Or ContactNo LIKE '%0%'
AND ClientTypeID = 2
答案 0 :(得分:6)
要排除CompanyName为null的所有行,您需要添加
AND CompanyName is NOT NULL
您可能需要在WHERE
之后和最后OR
条件之后使用括号
答案 1 :(得分:1)
天真地,只需使用
SELECT ClientID, CompanyName, FirstName, LastName, Street, City
FROM Client
WHERE CompanyName is not NULL and (CompanyName LIKE '%G%' Or FirstName LIKE '%J%' Or LastName LIKE '%J%' Or Street LIKE '%J%' Or City LIKE '%J%' Or ContactNo LIKE '%0%'
AND ClientTypeID = 2)
答案 2 :(得分:0)
CompanyName like '%G'
CompanyName
为null
,将失败,不是吗?
如果您的数据库不是那样,只需添加
即可 AND CompanyName IS NOT NULL
答案 3 :(得分:0)
SELECT ClientID, CompanyName, FirstName, LastName, Street, City
FROM Client
WHERE (CompanyName LIKE '%G%' Or FirstName LIKE '%J%' Or LastName LIKE '%J%' Or Street LIKE '%J%' Or City LIKE '%J%' Or ContactNo LIKE '%0%' AND ClientTypeID = 2) AND CompanyName IS NOT NULL;
问题是你有或有陈述。即使CompanyName不适合,LastName也可能适合。因为您在过滤结果中使用或将包括在内。
你正在混合或者是,并且两者都没有任何括号。你不应该这样做。你应该写一下:
SELECT ClientID, CompanyName, FirstName, LastName, Street, City
FROM Client
WHERE (CompanyName LIKE '%G%' Or FirstName LIKE '%J%' Or LastName LIKE '%J%' Or Street LIKE '%J%' Or City LIKE '%J%' Or ContactNo LIKE '%0%') AND ClientTypeID = 2;
或
SELECT ClientID, CompanyName, FirstName, LastName, Street, City
FROM Client
WHERE CompanyName LIKE '%G%' Or FirstName LIKE '%J%' Or LastName LIKE '%J%' Or Street LIKE '%J%' Or City LIKE '%J%' Or (ContactNo LIKE '%0%' AND ClientTypeID = 2);
答案 4 :(得分:0)
从所有列中删除所有空值
declare @ex as nvarchar(max) = ''
declare @_null as nvarchar(max)
set @_null = ''
select @_null += 'isnull(' + QUOTENAME(column_name) + ',0) as ' + QUOTENAME(column_name) + ','
from sample.INFORMATION_SCHEMA.columns where TABLE_NAME = 'table';
set @_null = left(@_null,len(@_null) - 1);
set @ex = 'select ' + @_null + ' from table';
exec sp_executesql @ex;