尝试以下解决方案
首先创建一个UDF,如下所示
alter function dbo.SplitString2(@inputStr varchar(1000),@del varchar(5))
RETURNS varchar(max)
As
BEGIN
DECLARE @t table(col1 varchar(100))
DECLARE @table table(col1 varchar(100))
DECLARE @ret varchar(max)
Set @ret = ''
INSERT INTO @t
select @inputStr
if CHARINDEX(@del,@inputStr,1) > 0
BEGIN
;WITH CTE1 as (
select ltrim(rtrim(LEFT(col1,CHARINDEX(@del,col1,1)-1))) as col,RIGHT(col1,LEN(col1)-CHARINDEX(@del,col1,1)) as rem from @t
union all
select ltrim(rtrim(LEFT(rem,CHARINDEX(@del,rem,1)-1))) as col,RIGHT(rem,LEN(rem)-CHARINDEX(@del,rem,1))
from CTE1 c
where CHARINDEX(@del,rem,1)>0
)
INSERT INTO @table
select col from CTE1
union all
select rem from CTE1 where CHARINDEX(@del,rem,1)=0
END
ELSE
BEGIN
INSERT INTO @table
select col1 from @t
END
Set @ret = (Select distinct col1 + ';' from @table for xml path(''))
return @ret
END
然后您可以根据您的要求运行以下选择/更新查询
更新查询——这将更新您表中的记录
update ArticleCategory Set ArticleCategories = dbo.SplitString2(ArticleCategories, ';' )
选择查询——这将只选择不同的记录
Select ArticleCategories , dbo.SplitString2(ArticleCategories, ';' ) from ArticleCategory