Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 How to filter duplicate values in In operator.

Author  Topic 

venkath
Posting Yak Master

202 Posts

Posted - 2006-09-25 : 03:41:51
Hi All,

How can i filter duplicate values in my where condition..

For ex:-

I have a variable str1 with values --'0,0,7,89,89,77,6,5,4,0,4'. this value is fetched through application.

Now i want to query as follows

str2='select * from x where y in '+ str1(str1 shuould have 0,7,89,77,6,5,4 with out duplicate values)

how can i achieve this..


Thanks



chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-09-25 : 03:52:28
[code]
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648

str2='select * from x Inner Join
(
Select distinct Data From dbo.split (',',' + str1 + ')
) as z
On z.Data = x.y '

[/code]

Chirag
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-09-25 : 04:03:46
Why does it matter if there are duplicate in the IN statement? You won't get any duplicate records because of it. (Bit inefficient perhaps, but taking them out is going to take some CPU power too!)

Kristen
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-09-26 : 02:35:25
Here is the procedure to remove the duplicates from the string based on the Seperator..


Create Function RemoveDuplicates
(
@sDuplicate varchar(8000),
@sSeperator char(1) = ','
)
Returns varchar(8000)
As
Begin
Declare @NoDuplicate varchar(8000)
Set @NoDuplicate = ''

if isnull(@sSeperator,'') = ''
Set @sSeperator = ','

Select @NoDuplicate = @NoDuplicate + INRow + ','
From
(
Select NullIf(SubString(@sSeperator + @sDuplicate + @sSeperator , IDNos ,
CharIndex(@sSeperator , @sSeperator + @sDuplicate + @sSeperator , IDNos) - IDNos) , '') AS INROW
FROM
(
SELECT top 100 percent a.i+b.i+c.i+d.i as IDNos FROM
(
Select 0 i union all Select 1 union all Select 2 union all Select 3 union all Select 4 union all Select 5 union all Select 6 Union All
Select 7 union all Select 8 union all Select 9 union all Select 10 union all Select 11 union all Select 12 Union All
Select 13 union all Select 14 union all Select 15
) as a,
(
Select 0 i union all Select 16 union all Select 32 union all Select 48 union all Select 64 union all Select 80 union all Select 96 Union All
Select 112 union all Select 128 union all Select 144 union all Select 160 union all Select 176 union all Select 192 Union All
Select 208 union all Select 224 union all Select 240
) as b,
(
Select 0 i union all Select 256 union all Select 512 union all Select 768 union all Select 1024 union all Select 1280 union all Select 1536 Union All
Select 1792 union all Select 2048 union all Select 2304 union all Select 2560 union all Select 2816 union all Select 3072 Union All
Select 3328 union all Select 3584 union all Select 3840
) as c,
(
Select 0 i union all Select 4096
) as d
order by 1
) as f
WHERE IDNos <= Len(@sSeperator + @sDuplicate + @sSeperator) AND
SubString(@sSeperator + @sDuplicate + @sSeperator , IDNos - 1, 1) = @sSeperator
AND CharIndex(@sSeperator , @sSeperator + @sDuplicate + @sSeperator , IDNos) - IDNos > 0
) as f
Group by INROW
Select @NoDuplicate = left(@NoDuplicate,len(@NoDuplicate) -1 )
return @NoDuplicate
End

GO

Select dbo.RemoveDuplicates('04,04,2005,2006,122,99012,04,78',',')


Chirag
Go to Top of Page
   

- Advertisement -