473,793 Members | 2,974 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

parsing a comma-separatinf list

AK
Once upon a time there was a table:

CREATE TABLE VENDOR(VENDOR_I D INT, NAME VARCHAR(50), STATE CHAR(2))@

in a while the developers realized that a vendor may be present in
xseveral states, so the structure was changed to

CREATE TABLE VENDOR(VENDOR_I D INT, NAME VARCHAR(50), STATE_LIST
VARCHAR(150))@

STATE)LIST could contain a list of comma-separated values like this:
CA,WA,OR
Not the best approach, but out here in the field things like this
happen all the time. Everybody were absolutely happy until somebody
required a query like this:
SELECT * FROM VENDOR WHERE STATE IN ('IL', 'WI','MI')
SELECT COUNT(*), STATE FROM VENDOR GROUP BY STATE
and so on
Given a dayabase structure developed by a skilled professional, that
would be a piece of cake, but not this time.

Moving to a better structure

CREATE TABLE VENDOR(VENDOR_I D INT, NAME VARCHAR(50))@
CREATE TABLE VENDOR_IN_STATE (VENDOR_ID INT, STATE CHAR(2))@

nobody wanted to change the front end application.
As a workaround, I created a view and INSTEAD OF triggers that worked
like this:

INSERT INTO VENDOR_IN_STATE _VIEW VALUES(1, 'ABC INC.', 'AK,AL,IL')@

SELECT * FROM VENDOR
VENDOR_ID NAME
----------- --------------------------------------------------
1 ABC INC.
1 record(s) selected.

SELECT * FROM VENDOR_IN_STATE
VENDOR_ID STATE
----------- -----
1 AK
1 AL
1 IL
3 record(s) selected.

I used recursion to define both the view and the triggers:

CREATE FUNCTION PARSE_LIST(C_LI ST VARCHAR(100))
RETURNS TABLE(TOKEN VARCHAR(100))
SPECIFIC PARSE_LIST
RETURN
WITH PARSED_LIST(STE P, TOKEN, REMAINDER,LEFTM OST_COMMA)
AS(
VALUES(0, '',C_LIST,3)
UNION ALL
SELECT STEP+1 AS STEP,
CASE WHEN LEFTMOST_COMMA> 0 THEN
CAST(LEFT(REMAI NDER,LEFTMOST_C OMMA-1) AS CHAR(2))
ELSE
REMAINDER
END AS TOKEN,
CASE WHEN LEFTMOST_COMMA> 0 THEN
CAST(SUBSTR(REM AINDER,LEFTMOST _COMMA+1) AS VARCHAR(100))
ELSE
NULL
END AS REMAINDER,
LOCATE(',',SUBS TR(REMAINDER,LO CATE(',',REMAIN DER)+1)) AS
LEFTMOST_COMMA
FROM PARSED_LIST
WHERE REMAINDER IS NOT NULL
)
SELECT TOKEN FROM PARSED_LIST WHERE STEP>0

I wrapped the recursive query in UDF so that it could be reused:

It is very easy to invoke this table UDF:

SELECT * FROM TABLE(PARSE_LIS T('AK,AR,IL,OH' )) AS PARSE_LIST
TOKEN
------
AK
AR
IL

OH

I user the UDF in an INSTEAD OF trigger

CREATE TRIGGER VENDOR_IN_STATE _I
INSTEAD OF INSERT
ON VENDOR_IN_STATE _VIEW
REFERENCING NEW AS N
FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC
DECLARE VENDOR_FOUND INT;
SET VENDOR_FOUND=(S ELECT COUNT(*) FROM VENDOR WHERE NAME=N.NAME);
IF NOT(VENDOR_FOUN D>0)
THEN
INSERT INTO VENDOR(VENDOR_I D, NAME) VALUES (N.VENDOR_ID, N.NAME);
END IF;
INSERT INTO VENDOR_IN_STATE (VENDOR_ID, STATE)
SELECT N.VENDOR_ID, PARSE_LIST.TOKE N FROM
TABLE(PARSE_LIS T(N.STATE_LIST) ) AS PARSE_LIST;
END @

I would really appreciate any feedback.
Are there any simpler approaches?
Nov 12 '05
15 5741
AK
jo*******@north face.edu (--CELKO--) wrote in message news:<a2******* *************** ****@posting.go ogle.com>...
What do you think? <<


Neat but too proprietary for my taste. Remember, I am the guy who
pushes pure SQL-92 code.


I would agree. However entry-level folks weren't able to understand
neither my original solution nor yours. This is why an
easy-to-understand one was necessary.

I'd say OLAP functions are very flexible and powerful. Maybe they
deserve getting into the standard?
Nov 12 '05 #11
To the best of my knowledge, ROW_NUMBER() is in the standard. AFAIK it
is alse supported by Oracle. So proprietary: Nope.
is the '+' in the standard for string types? Just curious....

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab

Nov 12 '05 #12
> To the best of my knowledge, ROW_NUMBER() is in the standard. AFAIK it
is alse supported by Oracle. So proprietary: Nope.
is the '+' in the standard for string types? Just curious....


Check out:

http://www.jtc1sc32.org/sc32/jtc1sc3...f?OpenDocument

The section on Window functions has all the relational OLAP stuff. I
noticed recursion and common table expressions were also there, all of
which work in Oracle and SQL Server (from Yukon on).

Christian.
Nov 12 '05 #13
AK
I would say that right now my top priority is to leave behind me code
that is easy to understand and maintain. This is where OLAP functions
are very handy. I love them.
I am less, if at all, concerned about portability and ANSI standards.

I think:
'abc'+'cde' will work on MS SQL Server , bot not on DB2 and not on
ORACLE
ROW_NUMBER() will work on DB@ and ORACLE but not on MS SQL Server
Nov 12 '05 #14
>> is the '+' in the standard for string types? <<

No, concatenation is an infixed || token. We copied it from PL/I.
What was funny is that Phil Shaw, the ANSI X3H2 representative from
IBM at the time did not like this symbol because he had worked with
PL/I and hated it.
Nov 12 '05 #15
> I would say that right now my top priority is to leave behind me code
that is easy to understand and maintain. This is where OLAP functions
are very handy. I love them.
I agree. The bonus with OLAP functions is they perform well over very
large result sets. The ANSI 92 equivalent won't.
I am less, if at all, concerned about portability and ANSI standards.


I'm more concerned with portability - but only between the major
vendors. ANSI appears to be a good way to get consistency in the
syntax across the vendors (there's nothing worse than seeing everyone
support a feature differently) - To me, the consistent nature of the
OLAP syntax is the best thing since vendors decided to support the
same outer join syntax.
Nov 12 '05 #16

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

Similar topics

8
5705
by: felciano | last post by:
Hi -- I am trying to use the csv module to parse a column of values containing comma-delimited values with unusual escaping: AAA, BBB, CCC (some text, right here), DDD I want this to come back as:
2
17947
by: GIMME | last post by:
I can't figure an expression needed to parse a string. This problem arrises from parsing Excel csv files ... The expression must parse a string based upon comma delimiters, but if a comma appears in double quotes it should not be used for parsing. For example in the simple case we'd have : $a='hello,brave,world';
8
1725
by: Darius Fatakia | last post by:
Hello, I have a file that I have opened for reading and this file contains lines with several different types of constraint information. For example, here are a few lines: length(0) = 10 Duration of task 0 is 10. needs(16,1) Operation 16 uses resource 1. before(49,9) Operation 49 must be before operation 9.
26
6879
by: SL33PY | last post by:
Hi, I'm having a problem parsing strings (comming from a flat text input file) to doubles. the code: currentImportDetail.Result = CType(line.Substring(7, 8).Trim(" "), System.Double) What is in my Watch:
2
2771
by: Trip | last post by:
Hello all, Is there anyway to store an array to MS Access 2002 without parsing the entire array row-by-row. For example, Oracle allows you to store BLOBs (binary large objects). I would like to be able to say in VB/VBA With rstName .AddNew !fieldName = ArrayName .Update
15
9560
by: VMI | last post by:
I'm parsing a comma-delimited record but I want it to do something if some of the string is between "". How can I do this? With the Excel import it does it correct. I'm using String.Split(). Basically, this is what I want to do: Use string.Split() on the whole string UNLESS the string is in between double-quotes. The part of the string in-between the "" will be ignored by String.Split Thanks.
4
1393
by: igotyourdotnet | last post by:
I have a question. I'm reading a CSV file that is uploading to my SQL db, I'm parsing out the file line by line. I'm getting the values and putting them into an arrayList seperate by commas. The problem I'm having is that one of the data values has commas in it so its blowing up on the other fields. How can I remove the commas from my string if they exist? example: Getting this BMW, Used, 325C, $19,252.00, Smith
4
1814
by: william | last post by:
Hello, I've imported an excel spreadsheet with a Name column which is formatted as Last, First, MI. Some examples I have in the Name column: Smith, Ellen P. Jones, Mary Jane Blackman-Pearson, Betsy D. Wright, George
9
1767
by: Jasper | last post by:
Hi, I have multiple data files which need parsing in realtime so high performance is *crucial*. I dont have a format definition, but from what I can see there is a hierarchy of data. Each data field is named thus <"name":(the <are mine). The data can be quoted text or unquoted text or a composite hierarcy field. Each name/data pair is terminated by a comma unless it is the last in the
4
1652
mickey0
by: mickey0 | last post by:
hello, I have to parsing a string like this: char * = "10 20 30 40"; and put its number into a vector<double> I thought to use strchr() and atof() and it's seems ok but to say the truth the line could be at times a little different: char * = "10 20 30 40 "; char * = "10,20,30,40"; char * = "10, 20,30, 40 "; char * = "10 20,30 40 "; all these above are valid line; instead this isn't': char * = "10 ...
0
9670
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
10430
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...
1
10159
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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
9033
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
7538
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
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4111
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

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.