Sql Server 2005中查询用分隔符分割的内容中是否包含其中一个内容

网络整理 - 08-20


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
-- =============================================
-- Author: <饭饭>
-- Create date: <2011-10-19>
-- Description: <查询分隔符分割的数据是否包含某项>
-- =============================================
*/
ALTER Function [dbo].[HasKey](@DataStr varchar(100),@key varchar(8))
returns int
as
begin
declare @return_str varchar(50)
declare @isok bit
declare @posi int
set @isok = 0
--set @DataStr = 'a,aaaa,aaaddd,ffass'
--set @key = 'aaaa'
set @DataStr = @DataStr+','
while len(@DataStr) > 0 and @isok=0
begin
set @posi= charindex(',',@DataStr)
set @return_str = replace(substring(@DataStr,1,charindex(',',@DataStr)),',','')
set @DataStr = substring(@DataStr,@posi+1,len(@DataStr))
if(@return_str=@key)
begin
set @isok=1
break
end
end
return @isok
end
GO