create table policies
(
policyno varchar(15)
);
insert into policies values ('500'),('501'),('502'),('503');
Declare @PolicyNos varchar(1500) = '500|503';
SELECT * FROM policies
WHERE policyno = ALL(SELECT item from dbo.fnSplit(@PolicyNos,'|'));
User is going to input multiple policies from front end and application sending multiple policies pipe separated as in @PolicyNos variable.
I'm using split function provided here https://stackoverflow.com/a/337752/435559 to check if all policies in @PolicyNos must exists then return true if any one exists but not the other then return false.
I Tried IN and EXISTS but they do OR operation inside but I want AND every policy exists then return true else false.
Edit -- Added In exists code which I tried
SELECT * FROM policies
WHERE policyno IN (SELECT item from dbo.fnSplit(@PolicyNos,'|'));
SELECT * FROM policies
WHERE exists (SELECT item from dbo.fnSplit(@PolicyNos,'|') where item = policies.policyno);