Doc_NO Ref1 Ref 2 Ref3
123 TAR Inv NULL
456 TAR NULL NULL
789 Inv NULL TAR
I need to check all three columns. If a doc_no has TAR in one column and null in the other two, I need it returned in my script result. In the case above, the script should only return Doc_no 456, as it is the only one that has TAR in one of the Ref columns and NULL in the remaining two.
select doc_no
from table1
where (ref1 = 'TAR' and ref2 is null and ref3 is null) or (ref1 is null and ref2 = 'TAR' and ref3 is null) or (ref1 is null and ref2 is null and ref3 = 'TAR')
SELECT doc_no
FROM table1
WHERE 'TAR' IN (Ref1, Ref2, Ref3) AND
CASE WHEN Ref1 IS NULL THEN 1 ELSE 0 END +
CASE WHEN Ref2 IS NULL THEN 1 ELSE 0 END +
CASE WHEN Ref3 IS NULL THEN 1 ELSE 0 END = 2