Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 11-Mar-22 8:18am

1 solution

There's few options:
1) Buit-in function: STRING_SPLIT[^]
2) Custom function
3) Common Table Expressions

More at: CP Articles: Search[^]
 
Share this answer
 
Comments
Member 15090354 14-Mar-22 2:49am    
Hi Maciej Los
I have tried with the below CTE
CREATE FUNCTION dbo.[UDF_Remove_Duplicate_EntryNew]
(
@Duplicate_Word VARCHAR(MAX)
)
RETURNS VARCHAR(MAX)
BEGIN
DECLARE @Xml XML
DECLARE @Removed_Duplicate_Word VARCHAR(MAX)
DECLARE @ReplaceInput VARCHAR(MAX);
SET @ReplaceInput = Replace(@Duplicate_Word,'|',' | ')
SET @Xml = CAST((''+REPLACE(@ReplaceInput,' ','')+'') AS XML)

;WITH CTE AS (
SELECT
ROW_NUMBER() OVER(ORDER BY A) AS [Sno],
A.value('.', 'varchar(max)') AS [Column]
FROM @Xml.nodes('A') AS FN(A) )

SELECT @Removed_Duplicate_Word =(SELECT Stuff((SELECT '' + ' ' + '' + A.[Column] FROM CTE A
LEFT JOIN CTE B ON A.[Sno]+1=B.[Sno]
WHERE (A.[Column]<>B.[Column] Or B.[Sno] is NULL)
FOR XML PATH('') ),1,1,''))
SET @Removed_Duplicate_Word = Replace(@Removed_Duplicate_Word,' | ','|')
RETURN @Removed_Duplicate_Word
END
GO

Working as expected but it is removing the immediately followed words only removing if any word having between 2 duplicate words not removing

Input: ABC|ABC a ABC ABC
Actual Output: ABC|ABC a ABC
but expected output: ABC|ABC a

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900