473,386 Members | 1,841 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,386 software developers and data experts.

Converting multiple values in form of comma separated string

5
Hi
I have a table which looks typically like this:-
ColA COLB
2370 2875
2370 3194
2376 3108
2417 2437
2539 2774
2539 2732
2539 3240

AS you can see from above ColB has repeated values. Fro example 2370 has 2875 and 3194 in ColB
What I am trying to do is

ColA COLB
2370 2875,3194
2376 3108
2417 2437
2539 2774,2732,3240

Can somebody guide me please
Cheers
Aug 1 '07 #1
2 2638
azimmer
200 Expert 100+
Hi
I have a table which looks typically like this:-
ColA COLB
2370 2875
2370 3194
2376 3108
2417 2437
2539 2774
2539 2732
2539 3240

AS you can see from above ColB has repeated values. Fro example 2370 has 2875 and 3194 in ColB
What I am trying to do is

ColA COLB
2370 2875,3194
2376 3108
2417 2437
2539 2774,2732,3240

Can somebody guide me please
Cheers
Here it goes, polish if you like (input table is Test, output goes to TestOut (int,varchar(255)):
Expand|Select|Wrap|Line Numbers
  1. declare @colA as int, @newcolA as int, @colB as int
  2. declare @newcolB as varchar(255)
  3. declare crsr cursor for select colA, colB from Test order by colA
  4. open crsr
  5. fetch next from crsr into @newcolA, @colB
  6. set @colA=@newcolA-1
  7. set @newcolB=''
  8. while @@FETCH_STATUS=0
  9. begin
  10.     if (@colA = @newcolA)
  11.     begin
  12.         set @newcolB = @newcolB + ',' + cast(@colB as varchar)
  13.     end
  14.     else
  15.     begin
  16.         if (@newcolB<>'') insert into TestOut values (@colA,@newcolB)
  17.         set @newcolB = cast(@colB as varchar)
  18.         set @colA = @newcolA
  19.     end
  20.     fetch next from crsr into @newcolA, @colB
  21. end
  22. if (@newcolB<>'') insert into TestOut values (@colA,@newcolB)
  23. close crsr
  24. deallocate crsr
  25.  
Aug 1 '07 #2
kuchel
5
thanks azimmer
I appreciate that.
Cheers,
Aug 2 '07 #3

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

Similar topics

3
by: jason | last post by:
How does one loop through the contents of a form complicated by dynamic construction of checkboxes which are assigned a 'model' and 'listingID' to the NAME field on the fly in this syntax:...
5
by: dnsstaiger | last post by:
I try to accomplish the following: I have two tables which are connected via a third table (N:N relationship): Table 1 "Locations" LocationID (Primary Key) Table 2 "Specialists"...
2
by: Jen F. | last post by:
I have inherited a medical database in which there are multiple values stored in a single field (ie. "Current Conditions" field might contain 1-20 different conditions, separated by comma (ie....
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
6
by: mark | last post by:
I have an asp.net ecommerce web application on a remote web server. I'm using an Access database on the back end. I've notice a few strange things. When I mimic an multiple user environment by...
2
by: Roy Rodsson via .NET 247 | last post by:
Hi all! I am using a stored procedure in SQL2000 for retrieving fileinformations from a db. the table as an uniqueidentifier for the file information. The stored procedure has a variable...
11
by: muttu2244 | last post by:
hi everybody i want to write a set of values to a file from python. For ex:: the fields name will "comp name", "ip addr", "mac addr" etc. And below all these fields i ll have the values for...
3
by: mahe23 | last post by:
All, How do One convert a comma separated column from a text file into rows in oracle. I have a scenario where the list of comma separated values changes dynamically. It is like: abc, ttt,...
4
by: sufian | last post by:
Below is the field where user enters his/her email address and the AJAX post request is sent to the server and the user sees the message: echo("<div id=\"message\" class=\"success\">Thank you! You...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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,...

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.