473,422 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,422 software developers and data experts.

How to Update fields in a record from other fields in the same record

I have a table similar to this:

Expand|Select|Wrap|Line Numbers
  1. Id     Number    Street      Address
  2. ---    ------    ------      -----------
  3. 100    NULL      NULL        234 Main St
  4. 101    NULL      NULL        516 W Oak St
  5.  
I want the end result to look like this:
Expand|Select|Wrap|Line Numbers
  1. Id     Number    Street      Address
  2. ---    ------    ------      -----------
  3. 100    234       Main St     234 Main St
  4. 101    516       W Oak St    516 W Oak St
  5.  
How can I do this in SQL? I understand I could do it with scripting but would prefer a SQL statement.

Thanks!
K
Jul 9 '10 #1

✓ answered by Jerry Winston

Try this out:

Expand|Select|Wrap|Line Numbers
  1. DECLARE @t as TABLE(StreetNumber int, StreetName  char(15), rData varchar(20))
  2.  
  3. INSERT INTO @t VALUES( NULL,NULL,'123 main st')
  4. INSERT INTO @t VALUES( NULL,NULL,'421 Cherry ln')
  5. INSERT INTO @t VALUES( NULL,NULL,'544 Peach st')
  6.  
  7. Update @t
  8.     SET StreetNumber = convert(INT,left(rData,charindex(' ',rData)-1)),
  9.     StreetName = right(rData,len(rData)-charindex(' ',rData))
  10.  
  11. SELECT * FROM @t
  12.  

3 1420
Where do you take the Number and Street values from?
Are they from another table? If yes, you may use INNER JOIN
Jul 10 '10 #2
Jerry Winston
145 Expert 100+
Try this out:

Expand|Select|Wrap|Line Numbers
  1. DECLARE @t as TABLE(StreetNumber int, StreetName  char(15), rData varchar(20))
  2.  
  3. INSERT INTO @t VALUES( NULL,NULL,'123 main st')
  4. INSERT INTO @t VALUES( NULL,NULL,'421 Cherry ln')
  5. INSERT INTO @t VALUES( NULL,NULL,'544 Peach st')
  6.  
  7. Update @t
  8.     SET StreetNumber = convert(INT,left(rData,charindex(' ',rData)-1)),
  9.     StreetName = right(rData,len(rData)-charindex(' ',rData))
  10.  
  11. SELECT * FROM @t
  12.  
Jul 11 '10 #3
@b0010100
Thanks b0010100! That does the trick.
Jul 12 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: el_sherif | last post by:
how to update and retrieve ntext fields in my asp code PLEASE HELP
1
by: middletree | last post by:
I have a table which has a datetime field for when a row in this one table (Ticket) was created. This is for trouble tickets for a help desk. After a ticket has been created, any additions to that...
1
by: mark.reichman | last post by:
First off.. Thanks to Grant Wagner for help in a previous thread related to this one. I am at a total loss... I have multiple fields in a form with the same name. Lets call the fields with the...
2
by: ruthboaz | last post by:
Hi, I am exporting a table with data to an excel file.. Some fields are empty in the exported file while the others and the key field, has values. The empty field(s) in excel, will be entered...
2
by: voroojak | last post by:
Hi How can i put save record and next record in one button. in my save record i put the calculation of the text boxes. i have a total field that the sum of the other field is in there and it wil be...
1
Kosal
by: Kosal | last post by:
Dear Sir/Madam I would like to Update record to last record in database that unkown last record please advice. thanks Best Regard
3
by: pzh20 | last post by:
I have an unbound form/subform where I populate a combo box on the main form, and using the onchange event, display fields from a table in a datasheet subform. I want to add a new record via the...
1
by: Trexsuz | last post by:
I am creating a subreport with multiple records possible. I am wanting one of 2 fields (A and B) to be visible based on the inormation in another field(C Yes or No). The first record has a Yes in C...
4
by: steve75043 | last post by:
Hello I hope someone can help with this. I need to subtract dates in current record from previous record for the current store. The key fields in the form are customer, store number and date picked...
0
TheSmileyCoder
by: TheSmileyCoder | last post by:
I have a report with some data on the left side, in fields that can grow, and some fields on the right side. My problem is that when the lefthand fields do grow, they "push" down my righthand fields....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.