473,405 Members | 2,338 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,405 software developers and data experts.

Divide a single column data to put into different rows.

112 100+
Hello there

I have a strange issue and just wanted to know if it is possible in SQL 2005. Can somebody please guide me on this.
I have a column with data like
223 245 356
223 356 222
223
456
Null
223 456
etc (these are 3 digit numbers ie; first row contains 3 different values and last row has 2 different values. )

Is there a way that I can divide this data and insert into different rows.
Example, in one row I have to insert the first value of the first column, 223 and in the next row I have to insert 245 and in the next 356 and so on. So the data present in this column would go into 10 rows. Am I clear?

Please let me know if this can be achieved. Thank you for your time.
Jan 21 '10 #1
4 6095
ck9663
2,878 Expert 2GB
Yes. You can parse that column and insert it into a table. Read the bottom part of this blog. You can use the same technique to parse your column and insert into a table. How big is your main table and what's the maximum number of value do you think your column has?

Happy Coding!!!

~~ CK
Jan 21 '10 #2
user1980
112 100+
thank you for your response, I read the blog but I am not sure I understand it completely.
My main table is a growing table and it would go to thousands. But I would actually use a stored procedure that would insert my data(table-1) daily into another table(table-2). For this insert I need to split the values. So on an average I would deal from 10 to 30 records per day.
My table-1 has these values and the no:of values in the column may range from 0 - 6/7.
Jan 21 '10 #3
ck9663
2,878 Expert 2GB
You have to parse your column and insert each column to your table-2. If you look at the blog, it uses comma (,) as delimiter and break a single string into multiple rows. If you think of your column as the single string, you can convert that into a resultset and insert it into a table.

Happy Coding !!!

~~ CK
Jan 21 '10 #4
nbiswas
149 100+
Try this(Xquery approach)

DECLARE @t TABLE(data VARCHAR(500))
INSERT INTO @t(data)
SELECT '223 245 356' UNION ALL
SELECT '223 356 222' UNION ALL
SELECT '223' UNION ALL
SELECT 456 UNION ALL
SELECT NULL UNION ALL
SELECT 223 456

DECLARE @tblDestinaiton(data VARCHAR(500))


Query:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO @tblDestinaiton(data)
  2. SELECT 
  3.  O.splitdata 
  4. FROM
  5.  (
  6.  SELECT *,
  7.  cast('<X>'+replace(F.data,',','</X><X>')+'</X>' as XML) as xmlfilter from @t F
  8.  )F1
  9.  CROSS APPLY
  10.  ( 
  11.  SELECT fdata.D.value('.','varchar(500)') as splitdata 
  12.  FROM f1.xmlfilter.nodes('X') as fdata(D)) O
Hope this helps
Mar 24 '10 #5

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

Similar topics

1
by: TimG | last post by:
12.) Now you have two different tables - each with two columns. Table #1 Single Column 2 rows with a value equal to 1 and 2
22
by: Bryan Guilliams | last post by:
I'm trying to come up with an elegant, simple way to compare two consecutive values from the same table. For instance: SELECT TOP 2 datavalues FROM myTable ORDER BY timestamp DESC That...
9
by: Stephen | last post by:
OK, I have filled a DataSet with an SqlDataAdapter and I just want the data in one column of one row. How can I do this since the following gives me the infamous 'Object reference not set to an...
2
by: RSH | last post by:
Hi, Iam struggling with an application where I am trying to transfer a datarow from one sql server to another instance of sql server. The schmeas may be slightly different and I am getting an...
5
by: BMeyer | last post by:
I have been losing my mind trying to parse an XML document (with nested child elements, not all of which appear in each parent node) into a DataGrid object. What I want to do is "flatten" the XML...
7
by: jason.langdale | last post by:
I have 3 tables I want to use in a view. Table A has field 1,2,3,4,5 and table B has field 1,2,3,4,5. I want to do a union on these. (I have done so successfully if I stop here) I also want to join...
7
by: Mintyman | last post by:
Hi, I'm working on a system migration and I need to combine data from multiple rows (with the same ID) into one comma separated string. This is how the data is at the moment: Company_ID ...
1
by: sqldba20 | last post by:
Folks: I need help with a script. I have 4 tables with AsOfDate, Country and FactorInt columns. In each table AsOfdate and Country are the same for that date but the FACTORINT column varies. I...
2
by: phpfreak2007 | last post by:
Hello all, I am Using One grid with Three column and I want to add Different Cell type in Same Column.. I can display properly with the error. code is as below.. ...
3
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
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,...

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.