473,511 Members | 16,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Split function in DB2 Version 5 Release 4

1 New Member
Hi,

I am new to Bytes and this is my first thread.

I know SQL Server but new to DB2. In my office we use IBM iSeries for Windows DB2 v5r4. I have written a function to split the string and return them as a table.

To be more clear in my requirement I will be parsing a string like
'ICECREAM (6) ~ CHOCOLATE (5) ~ PASTRY (2) ~' and I should get the particular value using the delimiter and the priority nos.( which is 6,5,3,2,1). Say in the above example I should display ICECREAM (6). This should be taken based on the priority nos.

I thank you in advance who could solve me out this issue.

My function:--


DROP FUNCTION SPLIT;

CREATE FUNCTION SPLIT(STRING VARCHAR(8000), DELIMITER CHAR(1))
RETURNS TEMPTABLE TABLE (ITEMS VARCHAR(8000), PRIORITY INTEGER)
LANGUAGE SQL
NOT DETERMINISTIC
MODIFIES SQL DATA
BEGIN

DECLARE IDX INT;
DECLARE SLICE VARCHAR(8000);

IF LENGTH (STRING) >= 1 OR STRING <> '' THEN
SET IDX = 1;
END IF;

WHILE IDX <> 0

SET IDX = LOCATE (DELIMITER, STRING);

IF IDX <> 0 THEN
SET SLICE = LEFT (STRING, IDX-1);
ELSE
SET SLICE = STRING;
END IF;

IF LENGTH (SLICE) > 0 THEN
INSERT INTO TEMPTABLE (ITEMS, PRIORITY) VALUES (SLICE, LEFT(RIGHT(TRIM(SLICE), 2),1));
END IF;

SET STRING = RIGHT(STRING, LENGTH(STRING) - IDX);

IF LENGTH (STRING) = 0 THEN
BREAK;
END IF;

END WHILE;

RETURN (SELECT ITEMS, PRIORITY FROM TEMPTABLE);

END;

Thanks
Vidhya
Feb 9 '10 #1
0 2561

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

Similar topics

11
2483
by: Carlos Ribeiro | last post by:
Hi all, While writing a small program to help other poster at c.l.py, I found a small inconsistency between the handling of keyword parameters of string.split() and the split() method of...
37
4946
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
0
4216
by: j | last post by:
Hi, Anyone out there with binary search tree experience. Working on a project due tomorrow and really stuck. We need a function that splits a binary tree into a bigger one and smaller one(for a...
18
8900
by: Bryan Parkoff | last post by:
"#define" can only be inside the global scope before main() function. "#if" can be tested after "#define" is executed. The problem is that "#define" can't be inside main() function. I do not wish...
6
2254
by: lovecreatesbeauty | last post by:
I ever missed a `return' statement when write a function `int HighDigit(Num)' to get the highest digit of an integer. But even if the `return' statement is ignored the function still can obtain...
11
21892
by: Ken Varn | last post by:
I want to be able to determine my current line, file, and function in my C# application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___ macros for getting this, but I cannot find a...
4
2503
by: Steven D'Aprano | last post by:
I'm having problems passing a default value to the maxsplit argument of str.split. I'm trying to write a function which acts as a wrapper to split, something like this: def mysplit(S, sep=None,...
2
5300
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
3
1821
by: Russ | last post by:
I have a Web Service that was originally created with .NET VC 2003, and subsequently converted to the 2005 version. It works fine when built as a debug version, and run on the workstation it was...
53
8316
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
0
7242
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
7355
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,...
1
7081
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...
0
7510
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...
0
5668
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,...
1
5066
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...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
447
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...

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.