Solved

Need to change column values in a database table

Posted on 2014-12-07
3
113 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now