鉴于是我的表结构,现在我需要编写一个查询来比较“STRVAL1和STRVAL2”然后“BVAL1和BVAL2”,如果两者不相同,那么应该返回该行。
预期结果......
请使用以下数据的文字说明
DECLARE @TAB TABLE (ID INT, STRVAL1 VARCHAR(8), STRVAL2 VARCHAR(8), BVAL1 BIT, BVAL2 BIT)
INSERT INTO @TAB VALUES
(1, NULL, NULL, NULL, NULL)
, (2, '', NULL, NULL, NULL)
, (3, '', '', NULL, NULL)
, (4, 'VAL', 'VAL', NULL, NULL)
, (5, 'VAL1', 'VAL2', NULL, NULL)
, (6, NULL, NULL, 0, NULL)
, (7, NULL, NULL, 0, 0)
, (8, NULL, NULL, 1, 1)
, (9, NULL, NULL, 1, 0)
SELECT ID, STRVAL1, STRVAL2, BVAL1, BVAL2
FROM @TAB
答案 0 :(得分:4)
你可以使用比较,虽然NULL
使这有点麻烦:
select t.*
from @tab t
where ((strval1 <> strval2) or
(strval1 is null and strval2 is not null) or
(strval1 is not null and strval2 is null)
) or
((bval1 <> bval2) or
(bval1 is null and bval2 is not null) or
(bval1 is not null and bval2 is null)
);
答案 1 :(得分:0)
您需要组合测试。
select ... from ... where
(strval1 is null and strval2 is not null) or
(strval1 is not null and strval2 is null) or
(strval1 != strval2) or
(bval1 is null and bval2 is not null) or
(bval1 is not null and bval2 is null) or
(bval1 != bval2)