Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 14th, 2004, 11:07 AM
Registered User
 
Join Date: Oct 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Update Table Help

I have a table that I need to update 3 separate fields from one field in the same table. I have an address field that I split apart and now need to move the remaining info into the appropriate fields. This I am assuming can be done with a Case or If statement.
Here is an example of what needs to be done. The column labeled address needs to be copied to Add1, Add2 or Add3. However if there is info already in Add1 then it needs to move to the next column Add2 and so on. Any help would be greatly appreciated.

Thanks,
Cy


 
Old October 14th, 2004, 03:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

something like:

update table set
add1 = case
 when add1 is not NULL then add1
 else substring(address, 1, 10) -- do whatever splitting of first line here
end,
add2 = case
  when add1 is not null then substring(address, 1, 10) -- first line
  else substring(address, 11, 20) -- second line of address
end,
add3 = case
  when add1 is not null then substring(address, 11, 20) -- second line
  else substring(address, 21, 30) -- third line
end
where address = @address

Something like that except you wouldn't use substring, you would use another function to split it apart. I don't know what your data is like so I can't suggest one.

Brian
 
Old October 14th, 2004, 04:44 PM
Registered User
 
Join Date: Oct 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Some of the data has carriage returns and some does not. And there is not a set number of letters or numbers between each carriage return. For example this is one line of an address entry:
"Department of Information Science
Saga University
1 Honjo
Saga 840-8502"

Some entries have a normal address entry:

3201 E. Ft. Lowell Rd. # 2002

Thanks,
Cy

 
Old October 15th, 2004, 08:07 AM
Authorized User
 
Join Date: Apr 2004
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How do you parse the data, then? How would you identify what should go in add1 from these two samples?

Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC
 
Old October 15th, 2004, 08:23 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

That will be tricky using just SQL. You may want to consider doing this at the application level, where you can do more heavy parsing.

Brian
 
Old October 15th, 2004, 02:19 PM
Registered User
 
Join Date: Oct 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How would i do that Brian?

Thanks,
Cy






Similar Threads
Thread Thread Starter Forum Replies Last Post
update table from another db table timbal25 SQL Server 2005 3 January 19th, 2008 06:47 AM
Update one table to another table using DTS in SQL Printmaker SQL Language 0 July 24th, 2007 07:17 AM
How to Update one table with other table data? ramk_1978 SQL Language 2 May 26th, 2006 12:51 AM
update table with an ID Num from different table scoobie PHP How-To 12 January 25th, 2005 12:28 PM
Update parent table with the sum of child table gbrown SQL Language 2 November 9th, 2004 07:53 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.