WHERE o.[Store Order Date] between @StartDate and @EndDate AND o.[Order Status Key] = 'F' AND EXISTS (Select 1 where @StoreID is null union all Select * from dbo.func_parseDelimitedString(@StoreID, ',') Parms where Parms.id = s.[External Store Id] ) AND EXISTS (Select 1 where @ExcludeStoreID is null union all Select * from dbo.func_parseDelimitedString(@ExcludeStoreID, ',') Parms where Parms.id <> s.[External Store Id] )

SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER FUNCTION [dbo].[func_parseDelimitedIntString](@paramString VARCHAR(MAX), @delimiter CHAR(1)) RETURNS @results_table TABLE (id BIGINT) AS BEGIN DECLARE @id VARCHAR(MAX) DECLARE @startPos int DECLARE @stringLength int WHILE LEN(@paramString) > 0 BEGIN SET @startPos = CHARINDEX(@delimiter, @paramString) IF @startPos < 0 SET @startPos = 0 SET @stringLength = LEN(@paramString) - @startPos - 1 IF @stringLength < 0 SET @stringLength = 0 IF @startPos > 0 BEGIN SET @id = SUBSTRING(@paramString, 1, @startPos - 1) SET @paramString= SUBSTRING(@paramString, @startPos + 1, LEN(@paramString) - @startPos) END ELSE BEGIN SET @id = @paramString SET @paramString = '' END INSERT @results_table(id) VALUES(CAST(@id AS BIGINT)) END RETURN END