SQL如何在where子句中包含和排除NULL值

时间:2017-04-25 07:16:51

标签: sql-server reporting-services

我的SSRS中有一个使用SP的@AgentGroup参数。

我想要做的是,如果参数传递值PREPB_STRSTR,NULL,它还将显示空值。我考虑在where子句中添加OR @AgentGroup is null,但如果参数只传递值PREPB_STRSTR,它也会显示空数据。

DECLARE @DateTo date = '3/21/2017',
@DateFrom date = '3/21/2017',
@VHGroup nvarchar(max) = 'PREPB_STRSTR,NULL'


  SELECT 
  [AgentIRACGroup]
  ,[CallOutCallID]
  ,[CallOutDateTime]
  ,[OutboundDuration]
  ,[CallOutNumber]
  ,[AgentName]
  ,[CallOutAgentID]
  ,[AgentGroup]
  ,[Outcome]
  ,[TalkTime]
   FROM [AgentStatisticsEOD].[dbo].[IRAC_Data]
   WHERE CONVERT(Date,[SCBCallDateTimeRequest]) BETWEEN @DateFrom AND @DateTo 
   AND [AgentGroup] collate database_default in (SELECT value from dbo.FnSplit(@VHGroup,',')) OR [AgentGroup] is null

即使[AgentGroup]没有传递空值,我的查询也会始终显示空@Agentgroup

1 个答案:

答案 0 :(得分:2)

解决它的最简单方法是添加另一个条件来检查参数是否实际包含单词NULL

DECLARE @DateTo date = '3/21/2017',
@DateFrom date = '3/21/2017',
@VHGroup nvarchar(max) = 'PREPB_STRSTR,NULL'


SELECT 
[AgentIRACGroup]
,[CallOutCallID]
,[CallOutDateTime]
,[OutboundDuration]
,[CallOutNumber]
,[AgentName]
,[CallOutAgentID]
,[AgentGroup]
,[Outcome]
,[TalkTime]
 FROM [AgentStatisticsEOD].[dbo].[IRAC_Data]
 WHERE CONVERT(Date,[SCBCallDateTimeRequest]) BETWEEN @DateFrom AND @DateTo 
 AND [AgentGroup] collate database_default in (SELECT value from dbo.Split(@VHGroup,',')) 
 OR 
 (',' + @VHGroup +',' LIKE '%,NULL,%' 
  AND [AgentGroup] is null
 )