Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
SteveH
Starting Member
27 Posts |
Posted - 2007-09-20 : 12:35:52
|
| Hi all!I have imported a table into SQL Server from a legacy program. Each record has a repeating sequence of similar fields. (Ex. Accnt1, Assesed1, Paid1, Accnt2, Assesed2, Paid2, etc.) I would like to take a single record and put data from these fields into a table that has the columns Accnt, Assesed, and Paid. I am doing this for easier use in a program I am developing in VB 2005. Can this be done in SQL or do I need to have help from some VB code? If it's possible, what might the SQL look like?ThanksSteve |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2007-09-20 : 12:50:59
|
| Hopefully yuo don't have too many Columns SteveINSERT INTO yourNewTableINSERT INTO yourNewTableSELECT Accnt1, Assessed1, Paid1 FROM yourOldTableUNION ALL SELECT Accnt2, Assessed2, Paid2 FROM yourOldTableUNION ALL SELECT Accnt3, Assessed3, Paid3 FROM yourOldTableUNION ALL SELECT Accnt4, Assessed4, Paid4 FROM yourOldTableUNION ALL SELECT Accnt5, Assessed5, Paid5 FROM yourOldTableUNION ALL SELECT Accnt6, Assessed6, Paid6 FROM yourOldTableUNION ALL SELECT Accnt7, Assessed7, Paid7 FROM yourOldTableUNION ALL SELECT Accnt8, Assessed8, Paid8 FROM yourOldTableUNION ALL SELECT Accnt9, Assessed9, Paid9 FROM yourOldTableUNION ALL SELECT Accnt10, Assessed10, Paid10 FROM yourOldTableJim |
 |
|
|
SteveH
Starting Member
27 Posts |
Posted - 2007-09-21 : 11:23:12
|
| Thanks, Jim. This is a great jumpstart!Steve |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-09-21 : 11:40:10
|
Here's a bit of code you can hack somewhat to get a list of the columns in an appropriate order, and then covert into the sort of code that JimF posted:SELECT COLUMN_NAMEFROM INFORMATION_SCHEMA.COLUMNSWHERE TABLE_NAME = 'yourOldTable' AND ( COLUMN_NAME LIKE 'Accnt[0-9]%' OR COLUMN_NAME LIKE 'Assessed[0-9]%' OR COLUMN_NAME LIKE 'Paid[0-9]%' )ORDER BY RIGHT(COLUMN_NAME, 1), CASE WHEN COLUMN_NAME LIKE 'Accnt[0-9]%' THEN 1 WHEN COLUMN_NAME LIKE 'Assessed[0-9]%' THEN 2 WHEN COLUMN_NAME LIKE 'Paid[0-9]%' THEN 3 ELSE 999 END Kristen |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|
|
|
|