2017-01-23 68 views
2

當我使用此查詢:在使用時不爲空不同的結果

SELECT * from [dbo].[CRA] 
where [gender] like 'null' 
     and [house] like 'null' 
     and [residenttime] is null 
     and [worktime] is null 
     and [loaiDN] like 'null' 
     and [depend] like 'null' 
     and [expGD] like 'null' 
     and [Grincome] like 'null' 
     and [dunocacTCTD] like 'null' 
     and [tinhtrangno] like 'null' 
     and [tgQHTD] like 'null' 
     and [soduTB] like 'null' 
     and [TlquaMB] like 'null' 

結果:81行

但使用此查詢時:去除

SELECT * from [dbo].[CRA] 
where [gender] not like 'null' 
     and [house] not like 'null' 
     and [residenttime] is not null 
     and [worktime] is not null 
     and [loaiDN] not like 'null' 
     and [depend] not like 'null' 
     and [expGD] not like 'null' 
     and [Grincome] not like 'null' 
     and [dunocacTCTD] not like 'null' 
     and [tinhtrangno] not like 'null' 
     and [tgQHTD] not like 'null' 
     and [soduTB] not like 'null' 
     and [TlquaMB] not like 'null' 

行的數是> 1000行

爲什麼這兩個結果是不同的?

+0

你知道布爾代數嗎? – GurV

+0

爲什麼使用不帶通配符的LIKE運算符?也可以使用等於和不等於。另外'null'是一個不是NULL值的字符串。你必須使用'IS NULL'和'IS NOT NULL'來檢查NULL是否等於equals,否則equals將始終返回false。 –

+0

它的cus只有兩個變量實際上在excel文件中沒有任何值,其他變量有「null」填充,所以我必須使用不同的命令 – QVT

回答

2

由於!(A and B) = !A or !BDe Morgan's law

對面將是

SELECT * from [dbo].[CRA] 
where [gender] not like 'null' 
     or [house] not like 'null' 
     or [residenttime] is not null 
     or [worktime] is not null 
     or [loaiDN] not like 'null' 
     or [depend] not like 'null' 
     or [expGD] not like 'null' 
     or [Grincome] not like 'null' 
     or [dunocacTCTD] not like 'null' 
     or [tinhtrangno] not like 'null' 
     or [tgQHTD] not like 'null' 
     or [soduTB] not like 'null' 
     or [TlquaMB] not like 'null' 

或簡單地,

SELECT * from [dbo].[CRA] 
where not ([gender] like 'null' 
     and [house] like 'null' 
     and [residenttime] is null 
     and [worktime] is null 
     and [loaiDN] like 'null' 
     and [depend] like 'null' 
     and [expGD] like 'null' 
     and [Grincome] like 'null' 
     and [dunocacTCTD] like 'null' 
     and [tinhtrangno] like 'null' 
     and [tgQHTD] like 'null' 
     and [soduTB] like 'null' 
     and [TlquaMB] like 'null'); 
+0

它是有道理的,但我的目標是刪除所有具有NULL值的記錄變量。並且只有兩個變量實際上在excel文件中沒有值,其他變量填充了「null」,所以我必須使用不同的命令 – QVT

+0

非常感謝。你的第二個查詢工作 – QVT

1

我相信你與你的線條比較一個字符串,如

and [house] not like 'null' 

相反假的報價關閉和使用NULL或NOT NULL

where Field is null 
where Field is not null 
0

下面是另一個樣品:

SELECT * from [dbo].[CRA] 
    where COALESCE(NULLIF([gender],'null') 
        ,NULLIF([house],'null') 
        ,[residenttime] 
        ,[worktime] 
        ,NULLIF([loaiDN],'null') 
        ,NULLIF([depend],'null') 
        ,NULLIF([expGD],'null') 
        ,NULLIF([Grincome],'null') 
        ,NULLIF([dunocacTCTD],'null') 
        ,NULLIF([tinhtrangno],'null') 
        ,NULLIF([tgQHTD],'null') 
        ,NULLIF([soduTB],'null') 
        ,NULLIF([TlquaMB],'null') 
        ) IS NULL --Will be returned only all the values is null or 'null' 

BUT

  where COALESCE(NULLIF([gender],'null') 
      ,NULLIF([house],'null') 
      ,[residenttime] 
      ,[worktime] 
      ,NULLIF([loaiDN],'null') 
      ,NULLIF([depend],'null') 
      ,NULLIF([expGD],'null') 
      ,NULLIF([Grincome],'null') 
      ,NULLIF([dunocacTCTD],'null') 
      ,NULLIF([tinhtrangno],'null') 
      ,NULLIF([tgQHTD],'null') 
      ,NULLIF([soduTB],'null') 
      ,NULLIF([TlquaMB],'null') 
      ) IS NOT NULL --It will be returned at least on value is not null('null')