Solved

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

Posted on 2014-12-08
3
72 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 45

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 31

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now