I have string values like below
'Is The string has | has duplicate duplicate |value abc abc| value Lmn Lmn'
Could you please assist on this how to get out llike below in sql server
Note in output | pipe symbol should be disply
'Is The string | has duplicate |value abc| Lmn'
What I have tried:
I have tried with the below funtion but the pipe symbol was also removed but I want maintain the pipe symbol between words after removing duplicate words also.
declare @text varchar(max) = 'Is The string has | has duplicate duplicate |value abc abc| value Lmn Lmn'
declare @i int = 1;
declare @nextCharacter varchar(max)
declare @word varchar(max)=''
declare @lastChar varchar(1) = substring(@text,len(@text),1)
--select @lastChar
declare @lastIndex varchar(max) = charindex(@lastChar,@text,len(@text))
select @lastIndex
--select len(@text)
create table #tmp (id int,word varchar(max))
while (@i <= len(@text))
begin
select @nextCharacter= substring(@text, @i, 1)
--select @nextCharacter,@i, @lastChar, len(@text)
if (@nextCharacter !=' ')
begin
select @word = @word + @nextCharacter
end
else
begin
insert into #tmp
values(@i,@word)
set @word=''
end
if (@i = len(@text) and @nextCharacter= @lastChar)
begin
insert into #tmp
values(@i,@word)
end
set @i = @i +1
end;
--select id,word from #tmp order by id;
WITH tblTemp as
(
SELECT ROW_NUMBER() Over(PARTITION BY word ORDER BY id)
As RowNumber,* FROM #tmp
) --select * from tblTemp
select * into #tmp2 FROM tblTemp where RowNumber =1
declare @newString varchar(max)=''
select @newString = @newString +word+' ' from #tmp2 order by id
select rtrim(@newString) as FromAddress
drop table #tmp2
drop table #tmp