Solved

Need to change column values in a database table

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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

742 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