範例資料如下
DECLARE @TEMPTALBE TABLE( NAME NVARCHAR(50), EMAIL NVARCHAR(50) ) INSERT INTO @TEMPTALBE(NAME)VALUES(‘小花’) INSERT INTO @TEMPTALBE(NAME)VALUES(‘小白’) INSERT INTO @TEMPTALBE(NAME,EMAIL)VALUES(‘小米’,’MAIL’) SELECT * FROM @TEMPTALBE WHERE EMAIL <> ‘MAIL’ 這時候會發現 SELECT * FROM @TEMPTALBE WHERE EMAIL <> ‘MAIL’ 會找不到任何資料,這就是因為NULL的關係 讓我深刻記下NULL要特別處理,示範解決方案 DECLARE @TEMPTALBE TABLE( NAME NVARCHAR(50), EMAIL NVARCHAR(50) ) INSERT INTO @TEMPTALBE(NAME)VALUES(‘小花’) INSERT INTO @TEMPTALBE(NAME)VALUES(‘小白’) INSERT INTO @TEMPTALBE(NAME,EMAIL)VALUES(‘小米’,’MAIL’) SELECT * FROM @TEMPTALBE WHERE ISNULL(EMAIL,”) <> ‘MAIL’ SELECT * FROM @TEMPTALBE WHERE IIF(EMAIL IS NULL, ” ,EMAIL) <> ‘MAIL’ SELECT * FROM @TEMPTALBE WHERE COALESCE(EMAIL,”) <> ‘MAIL’會發現這三種解法,都是將NULL轉換成空值,這樣有印象深刻嗎@@
沒有留言:
張貼留言