排除SQL Server中where子句中的null和其他值

时间:2017-08-29 22:25:21

标签: sql-server tsql

FROM 
    Table1 L
LEFT JOIN 
    Table2 M ON L.id = M.id
             AND ISNULL([Col1], '') NOT IN ('', '#N/A', 'NULL')

如何在T-SQL中编写此逻辑,我想要排除null,NA以及字符串NULL?

2 个答案:

答案 0 :(得分:0)

两种可能性是使用where子句(也是本主题的标题所示),或者删除left连接。

SELECT ...
  FROM Table1 L
  JOIN Table2 M ON L.id = M.id
   AND ISNULL([Col1],'') NOT IN ('','#N/A','NULL')

SELECT ...
  FROM Table1 L
  JOIN Table2 M ON L.id = M.id
 WHERE ISNULL([Col1],'') NOT IN ('','#N/A','NULL')

答案 1 :(得分:0)

SELECT      * 
FROM        Table1 L 
LEFT JOIN   Table2 M 
        ON     L.id = M.id 
           AND 
               M.id IS NOT NULL 
           AND 
               NOT(L.[Col1] IN ('#N/A', 'NULL')) 


SELECT      * 
FROM        (SELECT * FROM Table1 WHERE NOT(Table1.[Col1] IN ('#N/A', 'NULL'))) L
LEFT JOIN   Table2 M 
        ON      L.id = M.id 
            AND 
                M.id IS NOT NULL