检查SQL Server中三列中的null或空数据

时间:2014-10-31 09:12:27

标签: sql sql-server

我有一个包含三列的表格,我想检查列中的NullEmpty数据ImpMainComp

我使用以下查询:

SELECT Imp,Main,Comp
FROM Items_tbl
WHERE Contributor = 37 
 AND NULLIF(Imp, '') IS NULL
 AND NULLIF(Main, '') IS NULL
 AND NULLIF(Comp, '') IS NULL;

4 个答案:

答案 0 :(得分:2)

因此,如果您想COUNT行,您可以执行以下操作:

SELECT COUNT(*)
FROM Items_tbl
WHERE Contributor=37 
 AND NULLIF(Imp, '') IS NULL
 AND NULLIF(Main, '') IS NULL
 AND NULLIF(Comp, '') IS NULL;

或者,如果您只需要其中一列为NULL

SELECT COUNT(*)
FROM Items_tbl
WHERE Contributor=37 
 AND (NULLIF(Imp, '') IS NULL
      OR NULLIF(Main, '') IS NULL
      OR NULLIF(Comp, '') IS NULL
     );

答案 1 :(得分:0)

根据您对需要点名的评论......

SELECT Count(*)
  FROM Items_tbl
 WHERE  Contributor=37 and  ISNULL(Imp, '') = '' 
 and   ISNULL(Main, '') = '' 
 and  ISNULL(Comp, '') = '' ;

答案 2 :(得分:0)

嗯,我不确定我是否正确理解了这个问题,但是如果你想要3列的所有行都是NULL /空,你可以试试这个:

SELECT     ID, Contributor,
           Imp,
           Main,
           Comp
FROM       Items_tbl
WHERE      Contributor = 37
AND        (Imp IS NULL OR Imp = '')
AND        (Main IS NULL OR Main = '')
AND        (Comp IS NULL OR Comp = '')

你可以在这里看到 - > SQL fiddle

请注意,对于此查询,要么所有3列都应该全部为NULL或全部为空。

如果您希望所有上述列中的一行都为NULL /空的行,您可以使用`UNION'得到这个如下:

SELECT     ID, Contributor,
           Imp,
           Main,
           Comp
FROM       Items_tbl
WHERE      Contributor = 37
AND        (Imp IS NULL OR Imp = '')
AND        (Main IS NULL OR Main = '')
AND        (Comp IS NULL OR Comp = '')

UNION

SELECT     ID, Contributor,
           Imp,
           Main,
           Comp
FROM       Items_tbl
WHERE      Contributor = 37
AND        (Imp IS NULL)
OR         (Main IS NULL)
OR         (Comp IS NULL)

在此处查看 - > SQL fiddle

希望这有帮助!!!

答案 3 :(得分:-1)

试试这个。

修改

SELECT Imp,Main,Comp
    FROM Items_tbl
    WHERE  Contributor=37 AND COALESCE(NULLIF(ISNULL(Imp,''),''),NULLIF(ISNULL(Main,''),''),NULLIF(ISNULL(Comp,''),'')) is NULL