> On Monday, January 21, 2013 11:35:16 AM UTC+2, Martin Bowes wrote:
>> > >I don't think Ingres has a split function yet?
>
Here's a stored procedure I cobbled together (cribbed from somewhere else) that might be useful in
this context. It's written for DB2, so needs to be translated at least to some extent, and is
currently set up to process integers (carton numbers in the original context). I know I have used
a similar routine to do character fields without any problems.
YMMV.
Roger
create procedure hillr.test (IN p_string varchar(256))
-- p_string expected to be like '1,2,3'
-- this test proc splits out the comma separated list of
-- assumed integers, and presents them serially to the body of the proc.
language sql
specific test
begin
declare v_num integer default 1;
declare v_strlen integer;
declare v_carton integer;
declare v_wrkstr varchar(300);
declare v_substr varchar(300);
declare v_sqlstm varchar(256);
-- add a comma back and front, to eliminate first
-- and last character issues.
set v_wrkstr=',' || p_string || ',';
-- get the length of our working string
set v_strlen=length(v_wrkstr);
-- lopp though the string, getting in turn
-- each substring starting with the full string, and moving right
-- one character each time
while v_num <= v_strlen do
-- get the substring from the {v_num}th character to the end
select substr(v_wrkstr,v_num)
into v_substr
from sysibm.sysdummy1
;
-- assume we never have a zero carton number (could use -999 if needed)
set v_carton=0;
-- extract the carton number.
-- it's the substring starting from position 2
-- to the position of the next comma,
-- minus 2.
select cast(substr(v_substr,2,locate(',',v_substr,2)-2) as integer)
into v_carton
from sysibm.sysdummy1
-- only do this where our current substring
-- is more than one char(thus excluding the final comma)
-- and starts with a comma
where length(v_substr) > 1
and substr(v_substr,1,1)=',';
-- v_carton available here to do whatever you wish
-- so do something with it
set v_sqlstm='insert into hillrtest.tablex ' ||
'select ' ||
v_carton ||
' from sysibm.sysdummy1'||
' where '||
v_carton ||
' <> 0';
execute immediate v_sqlstm;
-- increment the counter to the next position in the string.
set v_num = v_num+1;
end while;
end
;
****************************************
Roger Hill
****************************************