Hi @Santosh Umarani ,
You could create a user-defined function as below:
CREATE FUNCTION [dbo].[SplitString]
(
@List NVARCHAR(MAX),
@Delim VARCHAR(255)
)
RETURNS TABLE
AS
RETURN ( SELECT [Value] FROM
(
SELECT
[Value] = LTRIM(RTRIM(SUBSTRING(@List, [Number],
CHARINDEX(@Delim, @List + @Delim, [Number]) - [Number])))
FROM (SELECT Number = ROW_NUMBER() OVER (ORDER BY name)
FROM sys.all_objects) AS x
WHERE Number <= LEN(@List)
AND SUBSTRING(@Delim + @List, [Number], LEN(@Delim)) = @Delim
) AS y
);
Then you could call this function as below:
declare @startdate date
declare @enddate date
declare @projectname varchar(1000)
set @startdate='2021-01-01'
set @enddate='2021-03-01'
set @projectname='project1,project2,project3'
select a.TestType,b.TestProjectName
from [dbo].[Reports] a
left join [dbo].[TestProjects] b on a.TestProjectID=b.TestProjectID
where a.TestDate between @startdate and @enddate
and b.TestProjectName in (SELECT value FROM [dbo].[SplitString](@projectname, ','));
Best regards,
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.