Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-12-08
3
Medium Priority
?
83 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1200 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 52

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 800 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

618 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