It should be an easy one
My data is
TABLE1
AccreditationStatus FacilityId FacilityName GroupAlphabet NodeName c_NodeId
-------------------------------------------------------------------------------------------------------------
Current 12359116 Addiction Medicine A Nepean 4
TABLE2
NodeId GroupNames RegPerSup FacCapacility
------------------------------------------------------------------------
4 A,B,X 1 2
My Query
select *
from table1 left join
table2 ast
on ast.nodeid = c_NodeId and
GroupAlphabet in (select items from SplitString(ast.GroupNames, ','))
returns all NULLs in second table
Current 12359116 Addiction Medicine A Nepean 4 NULL NULL NULL NULL
whereas if i try
select *
from table1 left join
table2 ast
on ast.nodeid = c_NodeId and
GroupAlphabet like '%' + ast.GroupNames + '%'
the results are correct for only exact matches but not comma separated groups
SplitString is a UDF that returns a table with items datatype as nvarchar (GroupAlphabet is type varchar if it matters)
My SplitString function is
ALTER FUNCTION [dbo].[SplitString](@String NVARCHAR(max), @Delimiter char(1))
returns @temptable TABLE (items NVARCHAR(max))
as
begin
declare @idx int
declare @slice NVARCHAR(max)
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