Update Duplicate Column With Null (T-SQL)




create table tableName (_g_id int identity(1,1), field1 varchar(50))
insert into tableName values ('aaa'),('aaa'),('ccc'),('aaa'),('bbb'),('bbb')

SELECT field1, Row_number() over(partition by field1 order by newid() ) rn

FROM tableName

;WITH mycte
     AS (SELECT field1,ROW_NUMBER()
                         OVER(
                           partition BY field1
                           ORDER BY NEWID() ) rn
         FROM   tableName)
UPDATE mycte
SET    field1 = NULL
WHERE  rn > 1

SELECT * FROM   tableName 


drop table tablename

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s