Solved

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

Posted on 2014-12-08
3
81 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 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 50

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
Viewers will learn how the fundamental information of how to create a table.

688 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