gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:49:02 24 4
gpt4 key购买 nike

我当前有一个 select 语句,用于检查多个列以查看它们是否有数据。如果它们中的任何一个为空,那么我想要将一个位设置为 false。如果它们都不为空,那么我希望将一个位设置为 true。这是我目前拥有的:

select
cast(
case when ChangeOrderNumber is null then 0 else 1 end *
case when ClientName is null then 0 else 1 end *
case when QuoteNumber is null then 0 else 1 end *
case when ClientNumber is null then 0 else 1 end *
case when ServiceLine is null then 0 else 1 end *
case when ServiceLineCode is null then 0 else 1 end *
case when GroupLeader is null then 0 else 1 end *
case when CreatedBy is null then 0 else 1 end *
case when PTWCompletionDate is null then 0 else 1 end *
case when BudgetedHours is null then 0 else 1 end *
case when BudgetDollars is null then 0 else 1 end *
case when InternalDeadlineDate is null then 0 else 1 end *
case when ProjectDescription is null then 0 else 1 end *
case when Sales is null then 0 else 1 end *
case when Coop is null then 0 else 1 end *
case when PassThrough is null then 0 else 1 end *
case when POStatus is null then 0 else 1 end *
case when PONumber is null then 0 else 1 end as bit
)
as Flag
from t

现在,该代码可以工作,但有点冗长,我想知道是否有人知道更好的方法来做到这一点。请注意,正在检查多种数据类型。

更多详情:此代码位于用于处理变更单的应用程序中正在查看的 View 中。在处理变更单之前,它必须满足一些数据质量检查的要求。该 View 显示所需数据是否为空。

最佳答案

只需将它们相加即可,因为 NULL +“某物”始终为 NULL ...

CREATE TABLE #test(column1 int,column2 varchar(4),column3 float)

INSERT #test VALUES(2,'2',2)
INSERT #test VALUES(0,'1',0)
INSERT #test VALUES(null,'1',0)
INSERT #test VALUES(1,null,0)
INSERT #test VALUES(0,'1',null)
INSERT #test VALUES(null,null,null)

SELECT CASE
WHEN column1 + column2 + column3 is NULL THEN 0 ELSE 1 END, *
FROM #test

来自post我在 3 年前创建...

请记住,如果您有非数字字符,则必须将其转换为 varchar ...

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

这里是转换,不需要转换varchar列

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

关于sql-server - 确定是否有任何值为 null,如果为 true,则为 false,否则为 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6116185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com