Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Check two delimited string items

Author  Topic 

walidm
Starting Member

1 Post

Posted - 2010-10-10 : 21:38:53
Hi all,
Which is the best way to check if an item or more of one delimited string exist in another?
I'd like to pass 2 delimited strings to a UDF and get back TRUE if at least one item of
the first delimited string exists in the second delimited string.

delStr1="US,FR,DE"
delStr2="GB,DE,CA,PL"
delStr3="CN,DK,IT"

UDF_CheckDelimitedStrings(delStr1,DelStr2)= TRUE (DE exists in both)
UDF_CheckDelimitedStrings(delStr2,DelStr3)= FALSE

Thanks in advance.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-10-10 : 21:53:44
use CSVTable or fnParseList to parse and use INNER JOIN or EXISTS to determine if one exists in another.


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

kunal.mehta
Yak Posting Veteran

83 Posts

Posted - 2010-10-11 : 04:03:54
CREATE FUNCTION dbo.Split(@String varchar(8000), @Delimiter char(1))
returns @temptable TABLE (items varchar(8000))
as
begin
declare @idx int
declare @slice varchar(8000)

select @idx = 1
if len(@String)<1 or @String is null return

while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String

if(len(@slice)>0)
insert into @temptable(Items) values(@slice)

set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end
Go to Top of Page
   

- Advertisement -