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
 General SQL Server Forums
 New to SQL Server Programming
 how to find duplicate records

Author  Topic 

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-05-11 : 17:24:44
How do i update duplicate email address?
I have to update Repit='Y' if there are duplicate email address on the table.

declare @tbl1 table
(ID INT,
Email varchar(12),
Repit varchar(1)
)

INSERT INTO @tbl1
SELECT 1, 'abc@aa.com',null

INSERT INTO @tbl1
SELECT 2, 'bb@bb.com',null

INSERT INTO @tbl1
SELECT 3, 'abc@aa.com',null

INSERT INTO @tbl1
SELECT 4, 'xyz@bb.com',null

Skorch
Constraint Violating Yak Guru

300 Posts

Posted - 2009-05-11 : 18:32:59
UPDATE @tbl1
SET Repit = 'Y'
WHERE Email IN (SELECT Email FROM @tbl1 GROUP BY Email HAVING COUNT(Email)>1)

Some days you're the dog, and some days you're the fire hydrant.
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-05-12 : 00:17:20
Hi try this once,

update t
set Repit = 'Y'
from ( select *,row_number() over ( partition by email order by id) as rd from @tbl1) as t
where t.rd > 1
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-05-12 : 01:49:08
update t
set repit ='y'
from( select repit, count(email)over (partition by email) as cnt from @tbl1
)t where cnt >1
select * from @tbl1
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-12 : 09:54:22
quote:
Originally posted by Nageswar9

Hi try this once,

update t
set Repit = 'Y'
from ( select *,row_number() over ( partition by email order by id) as rd from @tbl1) as t
where t.rd > 1


this will not update the first record's repit field for repeating email value
Go to Top of Page
   

- Advertisement -