怎样处理字符串‘aa bb cc aa dd aa ee cc’ 变成 ‘aa bb cc dd ee’ 将重复的保留一个
怎样处理字符串‘aa bb cc aa dd aa ee cc’ 变成 ‘aa bb cc dd ee’ 将重复的保留一个?
我有个字段保留放有如‘aa bb cc aa dd aa ee cc’ 这种有重复的值,我要将它去除重复,怎样处理?
------解决方案--------------------用sql实现有点困难,前台程序用split来处理更方便了
------解决方案--------------------一个一个的截取进一个临时表,然后在distinct然后在拼接出来新的那个,当然有点复杂,要弄个存储过程,里面使用while 循环
------解决方案--------------------
DECLARE @x varchar(100), @s varchar(100)
SET @x='aa bb cc aa dd aa ee cc'
set @s=''
;with cte
as
(
SELECT SUBSTRING(@x, number, CHARINDEX(' ',@x+' ',number)-number) AS [x], number
FROM master.dbo.spt_values
where [type]='p' AND number BETWEEN 1 AND LEN(@x)
AND SUBSTRING(' '+@x, number,1)=' '
)
select @s=@s+' '+x
from cte as A
where not exists(select 1 from cte where x=A.x and number<A.number)
order by number
select @s=stuff(@s,1,1,'')
print @s
------解决方案--------------------DECLARE @s varchar(100), @x XML
SET @s='aa bb cc aa dd aa ee cc'
set @x='<x>'+REPLACE(@s,' ','</x><x>')+'</x>'
select CAST(@x.query('distinct-values( data(/x) )
') as varchar(100))as result
------解决方案--------------------
declare @str varchar(1000)='aa bb cc aa dd aa ee cc'
declare @splitstr varchar(20)=' ';
;with t as
(
select @str as fstrold,SUBSTRING(@str,1,charindex(@splitstr,@str+@splitstr)) fstr
,SUBSTRING(@str,charindex(@splitstr,@str+@splitstr)+1,len(@str)) fsplit
union all