Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 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 June 1st, 2006, 01:53 AM
Registered User
 
Join Date: Jun 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Splitting table data

I have this data on a table:

Owner,Car1,Car2,Car3,...Car15,Model1,Model2,Model3 ,...Model15

I want to split the table elements to and save it to a new table:

Owner,Car1,Model1
Owner,Car2,Model2
Owner,Car3,Model3
...
Owner,Car15,Model15

How can I achieve this?

 
Old June 1st, 2006, 04:13 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

            SELECT Owner, Car1 Car, Model1 Model FROM YourTable
UNION ALL SELECT Owner, Car2, Model2 FROM YourTable
UNION ALL SELECT Owner, Car3, Model3 FROM YourTable
UNION ALL SELECT Owner, Car4, Model4 FROM YourTable
UNION ALL SELECT Owner, Car5, Model5 FROM YourTable
UNION ALL SELECT Owner, Car6, Model6 FROM YourTable
UNION ALL SELECT Owner, Car7, Model7 FROM YourTable
UNION ALL SELECT Owner, Car8, Model8 FROM YourTable
UNION ALL SELECT Owner, Car9, Model9 FROM YourTable
UNION ALL SELECT Owner, Car10, Model10 FROM YourTable
UNION ALL SELECT Owner, Car11, Model11 FROM YourTable
UNION ALL SELECT Owner, Car12, Model12 FROM YourTable
UNION ALL SELECT Owner, Car13, Model13 FROM YourTable
UNION ALL SELECT Owner, Car14, Model14 FROM YourTable
UNION ALL SELECT Owner, Car15, Model15 FROM YourTable

If you at the same time want to remove duplicates, omit ALL as

        SELECT Owner, Car1 Car, Model1 Model FROM YourTable
UNION SELECT Owner, Car2, Model2 FROM YourTable
UNION SELECT Owner, Car3, Model3 FROM YourTable
UNION SELECT Owner, Car4, Model4 FROM YourTable
UNION SELECT Owner, Car5, Model5 FROM YourTable
UNION SELECT Owner, Car6, Model6 FROM YourTable
UNION SELECT Owner, Car7, Model7 FROM YourTable
UNION SELECT Owner, Car8, Model8 FROM YourTable
UNION SELECT Owner, Car9, Model9 FROM YourTable
UNION SELECT Owner, Car10, Model10 FROM YourTable
UNION SELECT Owner, Car11, Model11 FROM YourTable
UNION SELECT Owner, Car12, Model12 FROM YourTable
UNION SELECT Owner, Car13, Model13 FROM YourTable
UNION SELECT Owner, Car14, Model14 FROM YourTable
UNION SELECT Owner, Car15, Model15 FROM YourTable
 
Old June 1st, 2006, 03:06 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

I would suggest changing the table structure. Have an owernes table and a related table of cars that they own.

 
Old June 1st, 2006, 03:36 PM
Registered User
 
Join Date: Jun 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank you very much Peso. How can I omit null values?
For example, car1 to car5 have values and car6 to car15 are nulls. How can I omit car6 to car15 and model6 to model 15?

@jbenson001, I would love to change the structure but I can't some third party application is using that type of structure that's is why I'm splitting it.

 
Old June 1st, 2006, 11:34 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Something like this?

            SELECT Owner, Car1 Car, Model1 Model FROM YourTable WHERE Car1 IS NOT NULL
UNION ALL SELECT Owner, Car2, Model2 FROM YourTable WHERE Car2 IS NOT NULL
UNION ALL SELECT Owner, Car3, Model3 FROM YourTable WHERE Car3 IS NOT NULL
UNION ALL SELECT Owner, Car4, Model4 FROM YourTable WHERE Car4 IS NOT NULL
UNION ALL SELECT Owner, Car5, Model5 FROM YourTable WHERE Car5 IS NOT NULL
UNION ALL SELECT Owner, Car6, Model6 FROM YourTable WHERE Car6 IS NOT NULL
UNION ALL SELECT Owner, Car7, Model7 FROM YourTable WHERE Car7 IS NOT NULL
UNION ALL SELECT Owner, Car8, Model8 FROM YourTable WHERE Car8 IS NOT NULL
UNION ALL SELECT Owner, Car9, Model9 FROM YourTable WHERE Car9 IS NOT NULL
UNION ALL SELECT Owner, Car10, Model10 FROM YourTable WHERE Car10 IS NOT NULL
UNION ALL SELECT Owner, Car11, Model11 FROM YourTable WHERE Car11 IS NOT NULL
UNION ALL SELECT Owner, Car12, Model12 FROM YourTable WHERE Car12 IS NOT NULL
UNION ALL SELECT Owner, Car13, Model13 FROM YourTable WHERE Car13 IS NOT NULL
UNION ALL SELECT Owner, Car14, Model14 FROM YourTable WHERE Car14 IS NOT NULL
UNION ALL SELECT Owner, Car15, Model15 FROM YourTable WHERE Car15 IS NOT NULL

Or, without duplicates,

            SELECT Owner, Car1 Car, Model1 Model FROM YourTable WHERE Car1 IS NOT NULL
UNION SELECT Owner, Car2, Model2 FROM YourTable WHERE Car2 IS NOT NULL
UNION SELECT Owner, Car3, Model3 FROM YourTable WHERE Car3 IS NOT NULL
UNION SELECT Owner, Car4, Model4 FROM YourTable WHERE Car4 IS NOT NULL
UNION SELECT Owner, Car5, Model5 FROM YourTable WHERE Car5 IS NOT NULL
UNION SELECT Owner, Car6, Model6 FROM YourTable WHERE Car6 IS NOT NULL
UNION SELECT Owner, Car7, Model7 FROM YourTable WHERE Car7 IS NOT NULL
UNION SELECT Owner, Car8, Model8 FROM YourTable WHERE Car8 IS NOT NULL
UNION SELECT Owner, Car9, Model9 FROM YourTable WHERE Car9 IS NOT NULL
UNION SELECT Owner, Car10, Model10 FROM YourTable WHERE Car10 IS NOT NULL
UNION SELECT Owner, Car11, Model11 FROM YourTable WHERE Car11 IS NOT NULL
UNION SELECT Owner, Car12, Model12 FROM YourTable WHERE Car12 IS NOT NULL
UNION SELECT Owner, Car13, Model13 FROM YourTable WHERE Car13 IS NOT NULL
UNION SELECT Owner, Car14, Model14 FROM YourTable WHERE Car14 IS NOT NULL
UNION SELECT Owner, Car15, Model15 FROM YourTable WHERE Car15 IS NOT NULL
 
Old June 2nd, 2006, 02:35 AM
Registered User
 
Join Date: Jun 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much peso. What's with the nick anyway? Just curious, its our currency.

 
Old June 2nd, 2006, 02:37 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by patingsadagat
 Thank you very much peso. What's with the nick anyway? Just curious, its our currency.
It has to do with my name, Peter Larsson. My nick was founded a long time ago...

 
Old June 2nd, 2006, 03:09 AM
Registered User
 
Join Date: Jun 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That's cool.
I used your sql query but it timed-out.
I came up with this:

Select @xOwner=Owner,
       @xCars=Car1+'|'+Car2+'|'+Car3+'|'+Car4+'|'+Car5+'| '+Car6+'|'+Car7+'|'+
       Car8+'|'+Car9+'|'+Car10+'|'+Car11+'|'+Car12+'|'+Ca r13+'|'+Car14+'|'+Car15,
       @xModel=Model1+'|'+Model2+'|'+Model3+'|'+ Model4+'|'+Model5+'|'+ Model6
       +'|'+Model7+'|'+Model8+'|'+ Model9+'|'+Model10+'|'+ Model11+'|'+
       Model12+'|'+ Model13+'|'+Model14+'|'+ Model15
From MyTable

Insert Into NewTable
    Select @xOwner,Value1,Value2
    from dbo.fn_Split(@xCars,@xModel,'|')

where dbo.fn_Split is a function that returns a table.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Updating one table with data from another table dirtdog22 Access VBA 1 January 21st, 2008 04:41 PM
Splitting columns and inserting to a Table praveennk84 SQL Server 2000 3 December 1st, 2006 08:16 AM
How to Update one table with other table data? ramk_1978 SQL Language 2 May 26th, 2006 12:51 AM
Splitting Table into more efficient structure MauiSpud Access 7 May 12th, 2006 07:35 AM





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