Solved

Need to change column values in a database table

Posted on 2014-12-07
3
119 Views
Last Modified: 2014-12-07
I have a database table shown below. The table actually has hundreds of records in it, so I am only showing 6 records.
In the code column, the same Code has been assigned to every record. Example : "ABC".
In reality, only the very first record should have a Code value of "ABC". Specifically only the record with the earliest
DateEntered should have the ABC code value. All other Code values should be unique.
I already have a function which generates unqique codes. I just need to figure out how to change each duplicate "ABC" code
(Except the first record with the earliest date).

Do I need to use a cursor to do this, or can this be done with an update statement? I am under a time crunch to get this done,
but am lost as to where to start. Can someone help me out? I would even be willing to pay someone for help on this.





select * from People

Social     Code  DateEntered
---------------------------------------
888659000  ABC  2014-12-07 08:20:58.407
666659700  ABC  2014-12-07 08:30:20.387
900659000  ABC  2014-12-07 08:32:10.320
778659000  ABC  2014-12-07 08:34:07.620
458659000  ABC  2014-12-07 08:35:18.853



Example of how the table might look after the Codes have been changed to unique values

Social     Code  DateEntered
---------------------------------------
888659000  ABC  2014-12-07 08:20:58.407
666659700  XBC  2014-12-07 08:30:20.387
900659000  VBC  2014-12-07 08:32:10.320
778659000  OIC  2014-12-07 08:34:07.620
458659000  NMB  2014-12-07 08:35:18.853
0
Comment
Question by:brgdotnet
  • 2
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 40485634
if you have a function to generate the "unique" value, you can do this indeed.
update t
  set code = yourfunction()
 from yourtable t
where t.DateEntered > ( select min(x.DateEntered) from yourtable x )

if this assigns the same value to all the records, you need to change the function to accept some argument (even if the function does not need the value as such), and change like this:

update t
  set code = yourfunction( newid() )
 from yourtable t
where t.DateEntered > ( select min(x.DateEntered) from yourtable x )

hope this helps
0
 
LVL 2

Author Comment

by:brgdotnet
ID: 40485737
Wow, you are amazing. Thank you so much. I will test it out now.
0
 
LVL 2

Author Closing Comment

by:brgdotnet
ID: 40485760
Your awesome. Thank you so much. I just really need to start thinking in Sql.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
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

776 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