Sql server 在CASE语句期间SQL检查空值

Sql server 在CASE语句期间SQL检查空值,sql-server,Sql Server,当我们得到预期的值时,下面的代码工作正常 case Criteria when ''Security_Agent'' then substring(criteriavalue,charindex(''['',criteriavalue,8)+1,charindex('']'',criteriavalue,14)-charindex(''['',criteriavalue,8)-1) when ''Agent'' then substring(criteriavalue

当我们得到预期的值时,下面的代码工作正常

  case Criteria    
  when ''Security_Agent'' then substring(criteriavalue,charindex(''['',criteriavalue,8)+1,charindex('']'',criteriavalue,14)-charindex(''['',criteriavalue,8)-1)    
  when ''Agent'' then substring(criteriavalue,charindex(''['',criteriavalue,8)+1,charindex('']'',criteriavalue,14)-charindex(''['',criteriavalue,8)-1)    
  when ''Arranger'' then substring(criteriavalue,charindex(''['',criteriavalue)+1,charindex('']'',criteriavalue)-charindex(''['',criteriavalue)-1)     
  when ''Group'' then criteriavalue
  else substring(criteriavalue,charindex(''['',criteriavalue)+1,charindex('']'',criteriavalue)-charindex(''['',criteriavalue)-1)     
  end as CriteriaValue  
但在某些情况下,上述标准值为空/空

如何更改以上内容,使其在不为null时显示值(表示CRITERIAVALUE字段)。。如果为空,则忽略子字符串

选项1: 添加WHEN语句作为案例中的第一个求值,以单独输出null

WHEN CriteriaValue IS NULL then 'WHATEVER YOU WANT NULLS TO BE'
备选案文2: 如果要用静态文本值替换空值,请使用
ISNULL(CRITERIAVALUE,'Some Text Here')
代替所有
CRITERIAVALUE

选项1: 添加WHEN语句作为案例中的第一个求值,以单独输出null

WHEN CriteriaValue IS NULL then 'WHATEVER YOU WANT NULLS TO BE'
备选案文2:
如果要用静态文本值替换空值,请使用
ISNULL(CRITERIAVALUE,'Some Text Here')
代替所有
CRITERIAVALUE

在检查标准之前检查标准值

case when criteriavalue is not null then
 case Criteria    
  when ''Security_Agent'' then substring(criteriavalue,charindex(''['',criteriavalue,8)+1,charindex('']'',criteriavalue,14)-charindex(''['',criteriavalue,8)-1)    
  when ''Agent'' then substring(criteriavalue,charindex(''['',criteriavalue,8)+1,charindex('']'',criteriavalue,14)-charindex(''['',criteriavalue,8)-1)    
  when ''Arranger'' then substring(criteriavalue,charindex(''['',criteriavalue)+1,charindex('']'',criteriavalue)-charindex(''['',criteriavalue)-1)     
  when ''Group'' then criteriavalue
  else substring(criteriavalue,charindex(''['',criteriavalue)+1,charindex('']'',criteriavalue)-charindex(''['',criteriavalue)-1)     
  end  
end as CriteriaValue 

检查标准之前,请先检查标准值

case when criteriavalue is not null then
 case Criteria    
  when ''Security_Agent'' then substring(criteriavalue,charindex(''['',criteriavalue,8)+1,charindex('']'',criteriavalue,14)-charindex(''['',criteriavalue,8)-1)    
  when ''Agent'' then substring(criteriavalue,charindex(''['',criteriavalue,8)+1,charindex('']'',criteriavalue,14)-charindex(''['',criteriavalue,8)-1)    
  when ''Arranger'' then substring(criteriavalue,charindex(''['',criteriavalue)+1,charindex('']'',criteriavalue)-charindex(''['',criteriavalue)-1)     
  when ''Group'' then criteriavalue
  else substring(criteriavalue,charindex(''['',criteriavalue)+1,charindex('']'',criteriavalue)-charindex(''['',criteriavalue)-1)     
  end  
end as CriteriaValue 

您也可以使用
iif
,如下所示:

case Criteria    
  when ''Security_Agent'' then substring(criteriavalue,charindex(''['',criteriavalue,8)+1,charindex('']'',criteriavalue,14)-charindex(''['',criteriavalue,8)-1)    
  when ''Agent'' then substring(criteriavalue,charindex(''['',criteriavalue,8)+1,charindex('']'',criteriavalue,14)-charindex(''['',criteriavalue,8)-1)    
  when ''Arranger'' then substring(criteriavalue,charindex(''['',criteriavalue)+1,charindex('']'',criteriavalue)-charindex(''['',criteriavalue)-1)     
  when ''Group'' then criteriavalue
  else iif(criteriavalue IS NULL, NULL, substring(criteriavalue,charindex(''['',criteriavalue)+1,charindex('']'',criteriavalue)-charindex(''['',criteriavalue)-1))     
  end as CriteriaValue

您也可以使用
iif
,如下所示:

case Criteria    
  when ''Security_Agent'' then substring(criteriavalue,charindex(''['',criteriavalue,8)+1,charindex('']'',criteriavalue,14)-charindex(''['',criteriavalue,8)-1)    
  when ''Agent'' then substring(criteriavalue,charindex(''['',criteriavalue,8)+1,charindex('']'',criteriavalue,14)-charindex(''['',criteriavalue,8)-1)    
  when ''Arranger'' then substring(criteriavalue,charindex(''['',criteriavalue)+1,charindex('']'',criteriavalue)-charindex(''['',criteriavalue)-1)     
  when ''Group'' then criteriavalue
  else iif(criteriavalue IS NULL, NULL, substring(criteriavalue,charindex(''['',criteriavalue)+1,charindex('']'',criteriavalue)-charindex(''['',criteriavalue)-1))     
  end as CriteriaValue

你为什么到处都有?此代码是否嵌入到您的应用程序中?对于手头的问题,你需要看看你的其他问题。你有两个标准。一个是>'',然后是另一个。我们使用'',因为这是动态SQL,为什么到处都有''?此代码是否嵌入到您的应用程序中?对于手头的问题,你需要看看你的其他问题。你有两个标准。一个是>'',然后是另一个。我们使用'',因为这是动态SQLOption 2是安全的。@Richard,如果您单击答案框右上角的小
HELP
问号,它会给您一些关于格式化答案(或问题)的指示。选项2是安全的。@Richard,如果您单击答案框右上角的小问号
HELP
,它将为您提供一些有关设置答案格式(或问题格式)的提示。