473,605 Members | 2,637 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 22037
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
12331
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 cases it fails and I am having difficulty
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 the "" fully.Could any body have sample code for the same so that it will be helfful for my...
5
16592
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
15017
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 string input parameter. Now if I define the function to take in a string array, it solves my...
25
31723
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
2882
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
64611
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, disregarding the syntax that stream contains. Parsing is actually including the syntax in order to make...
1
64080
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, disregarding the syntax that stream contains. Parsing is actually including the syntax in order to make...
0
1850
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
8004
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7934
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8425
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8288
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6743
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5886
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1541
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1271
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.