Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Enterprise
Expertise: Beginner
Aug 19, 2002

Remove Duplicates from Delimited String in T-SQL


Use this snippet to remove duplicates from a delimited string:
 
DECLARE @mystring varchar(1000), @myword varchar(50),@CachedString
varchar(2000)
set @CachedString=''
DECLARE @i int,@j int
SELECT @mystring = 'cat dog fox cat chicken hen goose cat bird dog'
SELECT @i = 0,@j = 0
IF SUBSTRING (@mystring, LEN (@mystring), 1) <> ' '
BEGIN
   SELECT @mystring = @mystring + ' '
END
SELECT @i = CHARINDEX (' ', @mystring, @i + 1)
WHILE @i > 0
BEGIN
   SELECT @myword = SUBSTRING (@mystring, @j+1, (@i - @j) -1)
   if charindex(@myword,@CachedString)=0
    begin
    set @CachedString=@CachedString + @myword + ' '
    end
   SELECT @j = @i
   select @i = CHARINDEX (' ' , @mystring, @i + 1)
END

print left(@CachedString,len(@CachedString)-1)
Harinatha Reddy
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date