2013年2月21日 星期四

腦力退化....null 值比較

今天在informix 做一個簡單的查詢時竟然對這個查詢結果懷疑了起來

create table test(col1 char(1) not null, col2 char(1));

insert into test values ('1' ,null);
insert into test values ('1' ,'1');

select * from test where col2 <> '1' ;

原本以為會回傳第一筆資料. 但卻回傳0筆.

以為是informix才會這樣,結果到sql server也是如此.

我的頭腦真是老化了,怎麼懷疑起這樣的結果呢.

null 與任何值比較應都是無法比的.

欄位值有null先轉型再比較唄

--tsql
select * from test where coalesce(col2,'') <> '1' ;

--informix
select * from test where nvl(col2,'') <> '1' ;


參考NULL 值說明

沒有留言:

張貼留言

IIS 網站無法下載中文檔名檔案因為取消高位元字元

 網址+中文檔案名稱方式下載檔案顯示 404 - 找不到檔案或目錄。 但英數字檔案名稱則可下載。 因為設定GCB IIS,其中1項 將高位元字元預設取消勾選了。 27 TWGCB-04-014-0028 要求篩選與其他限制模組 允許高位元字元 這項原則設定決定查詢...