Click here to Skip to main content
15,902,636 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Friends,

I have a table in which one of the columns is having comma separated values which may be duplicate. I want to remove all the duplicate items from the column while selecting the data from this table as i required to export the same data into excel from the datatable filled from the selected table.

Please help.

For more clarity on this please don't hesitate to ask.

Thanks

Varun Sareen
Posted

IF you can access data in the cell, you can replace the duplicate content very easily by using RegEx.
Try str = Regex.Replace(cellContent, @"(\b\w+\b)\s+(\1(\s+|$))+", "$1 ");.
 
Share this answer
 
Comments
AshishChaudha 12-Jul-12 4:13am    
Good answer. +5
Abhinav S 12-Jul-12 4:14am    
Thanks.
SQL
CREATE FUNCTION [dbo].[Split](@String varchar(8000), @Delimiter char(1))
returns @temptable TABLE (items varchar(8000))
as
begin
    declare @idx int
    declare @slice varchar(8000)
 
    select @idx = 1
        if len(@String)<1 or @String is null  return
 
    while @idx!= 0
    begin
        set @idx = charindex(@Delimiter,@String)
        if @idx!=0
            set @slice = left(@String,@idx - 1)
        else
            set @slice = @String
 
        if(len(@slice)>0)
            insert into @temptable(Items) values(@slice)
 
        set @String = right(@String,len(@String) - @idx)
        if len(@String) = 0 break
    end
return
end

above function will return you the subject

execute the function as below:
SQL
select Split('2,3,4',',')

You will get all the records rowswise from above function
Then you can delete by executing
SQL
Delete from [tablename] where [id]=[your value]



Thanks
Ashish

Mark it answer if this is your solution so that other can refer to the solution.
 
Share this answer
 

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