Solved

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

Posted on 2014-12-08
3
79 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 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 48

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to export SQL to firebase ? 7 67
SQL Backup Question 2 29
TSQL convert date to string 4 34
SQL View / Qtry 3 8
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

809 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