?
Solved

Need to change column values in a database table

Posted on 2014-12-07
3
Medium Priority
?
134 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
[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
  • 2
3 Comments
 
LVL 143

Accepted Solution

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

770 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