473,549 Members | 2,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to parse a string column with comma delimited

N
Hi,
I would like to parse out each value that is seperated
by a comma in a field and use that value to join to another table.
What would be the easiest way to do so without having to
write a function or routine ?

EX.

Table AAA
COL1 COL2
1 11, 124, 156
2 11, 505, 600, 700, ...

Table BBB
COL1 COL2
11 Desc11
124 Desc124
156 Desc 156
Jul 20 '05 #1
2 22032
N (N@N.COM) writes:
I would like to parse out each value that is seperated
by a comma in a field and use that value to join to another table.
What would be the easiest way to do so without having to
write a function or routine ?

EX.

Table AAA
COL1 COL2
1 11, 124, 156
2 11, 505, 600, 700, ...


I hope that you understand that this is a horrible table design, and
if you have any possibility you should change it.

There is article on my web site,
http://www.sommarskog.se/arrays-in-sql.html, where I present a number
of ways to unpack lists into tables. I mainly discuss this in the context
of the list being one single list, sent down to the client. I very
briefly discuss this in the section "Unpacking a Table Column" and make
some suggesting about using a Numbers table. That is, rather than
using the function duo_text_split_ me, you should take the inner parts
of it, and apply the logic to your table.

--
Erland Sommarskog, SQL Server MVP, so****@algonet. se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
N,
Hi,
I would like to parse out each value that is seperated
by a comma in a field and use that value to join to another table.
What would be the easiest way to do so without having to
write a function or routine ?

EX.

Table AAA
COL1 COL2
1 11, 124, 156
2 11, 505, 600, 700, ...

Table BBB
COL1 COL2
11 Desc11
124 Desc124
156 Desc 156
.
.
.


You can do this with a table of seqential numbers.

-- You will need an auxiliary table of numbers.
-- Any table with enough rows will do to create it.
-- The number of rows must be greater than the length
-- of your longest string.

select top 1000 identity(int, 1, 1) Nbr
into Seq from master..syscolu mns
go
alter table Seq add primary key (Nbr)
go
create Table AAA (
COL1 int, COL2 varchar(200)
)
insert AAA values(1, '11, 124, 156')
insert AAA values(2, '11, 505, 600, 700')

create Table BBB (
COL1 int, COL2 varchar(200)
)
insert BBB values(11, 'Desc11')
insert BBB values(124, 'Desc124')
insert BBB values(156, 'Desc 156')
insert BBB values(600, 'Desc 600')
go

--Parse the string in AAA.COL2
select AAA.COL1,
cast(substring( List, Nbr + 1,
charindex(',', List, Nbr + 1) - (Nbr + 1))
as int) Item
from (select COL1, ',' + replace(COL2, ' ', '')+ ','
from AAA) AAA (Col1, List)
join Seq on substring(List, Nbr, 200) like ',_%'
and Nbr between 1 and len(List)
-- Or if you need to get the description from table BBB,
-- you can do it all in one step, without using a #temp table.

select AAA.COL1, AAA.Item, BBB.COL1, BBB.COL2
from (select AAA.COL1,
cast(substring( List, Nbr + 1,
charindex(',', List, Nbr + 1) - (Nbr + 1))
as int) Item
from (select COL1, ',' + replace(COL2, ' ', '')+ ','
from AAA) AAA (Col1, List)
join Seq on substring(List, Nbr, 200) like ',_%'
and Nbr between 1 and len(List)) AAA
join BBB
on BBB.COL1 = AAA.Item
go
drop table AAA
drop table BBB
drop table Seq

/***
COL1 Item
----------- -----------
1 11
1 124
1 156
2 11
2 505
2 600
2 700

COL1 Item COL1 COL2
----------- ----------- ----------- -------------
1 11 11 Desc11
1 124 124 Desc124
1 156 156 Desc 156
2 11 11 Desc11
2 600 600 Desc 600

***/

-- Linda

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
12320
by: MW | last post by:
Dear All Does anyone have a regular expression to parse a comma delimited line with some fields optionally having string delimiters (text qualifiers) I am currently testing with this regular expression and it works in almost all my test cases. I found this on the internet in a C# solution. ,(?=(*"*")*(?!*")) However in some of my test...
22
872
by: Ram Laxman | last post by:
Hi all, I have a text file which have data in CSV format. "empno","phonenumber","wardnumber" 12345,2234353,1000202 12326,2243653,1000098 Iam a beginner of C/C++ programming. I don't know how to tokenize the comma separated values.I used strtok function reading line by line using fgets.but it gives some weird behavior.It doesnot stripout...
5
16588
by: Theresa Hancock via AccessMonster.com | last post by:
I have an Excel table I need to import into Access. The name is entered into one field "Name". I'd like to have two fields in Access, FirstName and LastName. How do I do this. -- Message posted via http://www.accessmonster.com
14
15011
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in a comma-delimited string or string array. I don't want it to be a string because I want to overload this function, and it's sister already uses a...
25
31716
by: electrixnow | last post by:
in MS VC++ Express I need to know how to get from one comma delimited text string to many strings. from this: main_string = "onE,Two,Three , fouR,five, six " to these: string1 = "one"
29
2879
by: gs | last post by:
let say I have to deal with various date format and I am give format string from one of the following dd/mm/yyyy mm/dd/yyyy dd/mmm/yyyy mmm/dd/yyyy dd/mm/yy mm/dd/yy dd/mmm/yy mmm/dd/yy
5
64600
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called this article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts,...
1
64031
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts,...
0
1846
by: Kristi | last post by:
I need to create a CL program that will take a PF, and create a tab delimited file that has comma seperated column headings as the first record. I know i can use cpytostmf/cpytoimpf to create the file, but how do i get the headings to be comma seperated while the rest of the file is tab delimited? Any help would be wonderful. Thank you
0
7542
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7467
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7736
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6066
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3514
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3494
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1961
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1079
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.