sql-server – 确定是否有任何值为null,如果为true则为false,否则为true

前端之家收集整理的这篇文章主要介绍了sql-server – 确定是否有任何值为null,如果为true则为false,否则为true前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前有一个select语句,它检查几列以查看它们是否有数据.如果它们中的任何一个为null,那么我想要一个设置为false.如果它们都不为null,那么我想要一个设置为true.这是我现在拥有的:
  1. select
  2. cast(
  3. case when ChangeOrderNumber is null then 0 else 1 end *
  4. case when ClientName is null then 0 else 1 end *
  5. case when QuoteNumber is null then 0 else 1 end *
  6. case when ClientNumber is null then 0 else 1 end *
  7. case when ServiceLine is null then 0 else 1 end *
  8. case when ServiceLineCode is null then 0 else 1 end *
  9. case when GroupLeader is null then 0 else 1 end *
  10. case when CreatedBy is null then 0 else 1 end *
  11. case when PTWCompletionDate is null then 0 else 1 end *
  12. case when BudgetedHours is null then 0 else 1 end *
  13. case when BudgetDollars is null then 0 else 1 end *
  14. case when InternalDeadlineDate is null then 0 else 1 end *
  15. case when ProjectDescription is null then 0 else 1 end *
  16. case when Sales is null then 0 else 1 end *
  17. case when Coop is null then 0 else 1 end *
  18. case when PassThrough is null then 0 else 1 end *
  19. case when POStatus is null then 0 else 1 end *
  20. case when PONumber is null then 0 else 1 end as bit
  21. )
  22. as Flag
  23. from t

现在,该代码有效,但它有点冗长,我想知道是否有人知道更好的方法来做到这一点.请注意,有几种数据类型正在检查中.

更多详情:
代码位于应用程序中用于处理变更单的视图中.在处理变更单之前,它必须满足一些数据质量检查.此视图显示是否有任何所需数据为空.

解决方法

只需添加它们,因为NULL“something”总是为NULL …
  1. CREATE TABLE #test(column1 int,column2 varchar(4),column3 float)
  2.  
  3. INSERT #test VALUES(2,'2',2)
  4. INSERT #test VALUES(0,'1',0)
  5. INSERT #test VALUES(null,0)
  6. INSERT #test VALUES(1,null,0)
  7. INSERT #test VALUES(0,null)
  8. INSERT #test VALUES(null,null)
  9.  
  10. SELECT CASE
  11. WHEN column1 + column2 + column3 is NULL THEN 0 ELSE 1 END,*
  12. FROM #test

从我在3年前创建的post开始……

请记住,如果您的字符不是必须转换为varchar的数字…

  1. INSERT #test VALUES(0,'abc',null)

这是转换,无需转换varchar列

  1. SELECT CASE WHEN CONVERT(VARCHAR(100),column1)
  2. + column2
  3. +CONVERT(VARCHAR(100),column3) is NULL THEN 0 ELSE 1 END,*
  4. FROM #test

猜你在找的MsSQL相关文章