Solved

How do I change only the first occurence of a record?

Posted on 2014-12-08
3
78 Views
Last Modified: 2014-12-09
Say I have the table listed below with social security numbers and a code.
If there is not a code, then the field simply has four pound signs in it ####

Suppose I want to find the very first record that contains a #### in it's field,
and then change that CODE to the letters 'AAAA'. How can I do that?
I know I would perform a select statement, and then probably get the SSN in order to have a handle
on the selected row. Can someone help me out here?

BEFORE :

SSN          CODE
433789000    DFGH
344444444    DAAS
234324234    ####
432432443    ####
553789000    ####
224444444    ####
994324234    ####
882432443    ####


AFTER

SSN          CODE
433789000    DFGH
344444444    DAAS
234324234    AAAA
432432443    ####
553789000    ####
224444444    ####
994324234    ####
882432443    ####
0
Comment
Question by:brgdotnet
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 300 total points
ID: 40488258
there are several ways to do that... here is the simplest one

SET ROWCOUNT 1
UPDATE yourtable SET CODE = 'AAAA' WHERE code = '####'
SET ROWCOUNT 0

if the SSN is unique:
UPDATE t
  SET code = 'AAAA'
FROM yourtable t
where SSN = ( SELECT TOP 1 x.SSN FROM yourtable x where x.code = '####' )
  and code = '####'

using a CTE this is also possible:
;WITH data as ( select ssn, code, row_number() over ( order by ssn) rn from yourtable where code = '####' )
update data set code = 'AAAA' where rn = 1
0
 
LVL 47

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 200 total points
ID: 40488668
You need to explain how to get the first record with CODE='####'.
Assuming that is the one with lower SSN, then you can use this update statement:
UPDATE TableName
SET CODE = 'AAAA'
WHERE SSN = (SELECT MIN(SSN) FROM TableName WHERE CODE = '####')

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 40488980
What defines the very first occurrrence of a record? There is no guarantee that a select statement will return records in a specific order without an order by clause. You show the after as updating the code for ssn 234324234 to 'AAAA', but that ssn is not the first (or last) ssn if that's what is being ordered by. Furthermore, look at the order for the following queries (a table name codes was created from your sample data):
select * from codes;
       SSN CODE
---------- ----
 433789000 DFGH
 344444444 DAAS
 234324234 ####
 432432443 ####
 553789000 ####
 224444444 ####
 994324234 ####
 882432443 ####
 select * from codes
 order by code desc;
       SSN CODE
---------- ----
 433789000 DFGH
 344444444 DAAS
 553789000 ####
 224444444 ####
 994324234 ####
 882432443 ####
 234324234 ####
 432432443 ####

select * from codes
order by code asc;
       SSN CODE
---------- ----
 432432443 ####
 994324234 ####
 234324234 ####
 882432443 ####
 553789000 ####
 224444444 ####
 344444444 DAAS
 433789000 DFGH

select * from codes
order by ssn asc;
       SSN CODE
---------- ----
 224444444 ####
 234324234 ####
 344444444 DAAS
 432432443 ####
 433789000 DFGH
 553789000 ####
 882432443 ####
 994324234 ####

The "first occurrence" of a record with a code of '####' is different in every case.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question