Subject | Re: [firebird-support] Re: possible to split a delimited string? |
---|---|
Author | Helen Borrie |
Post date | 2008-03-01T04:46:44Z |
At 11:27 AM 1/03/2008, you wrote:
substring (whatever from 1 for n). If you're not using Fb 2.1, you'll also need to declare the UDF strlen() to your database.
create procedure breakapart(
inputstring varchar(99) /* same length as your list input string */,
separator char
)
returns (element varchar(3) /* max length of a list item */,
outputstring varchar(99) /* same length as inputstring */,
lengthremaining smallint)
as
declare variable anychar char;
declare variable counter smallint;
begin
element = '';
counter = 0;
while (anychar <> separator ) do
begin
counter = counter + 1;
anychar = substring(inputstring from 1 for 1);
if (anychar <> separator) then
element = element || anychar;
inputstring = substring (inputstring from (counter + 1));
end
outputstring = inputstring;
lengthremaining = strlen(outputstring);
end ^
Call this from your procedures that need to process a list, as per the following example:
create procedure process_my_list (
aSeparatedList varchar(99),
whatever_else_you_want)
....
as
declare variable anElement varchar(3);
declare ListCurrentLength smallint;
begin
ListCurrentLength = strlen(aSeparatedList);
while (ListCurrentLength > 0) do
begin
execute procedure breakapart (aSeparatedList)
returning_values(:anElement, :aSeparatedList, :ListCurrentLength);
-- DO
-- ALL THE STUFF
-- YOU WANT TO DO
-- WITH
-- THE CURRENT
-- LIST ELEMENT
end
end ^
./heLen
>Thanks Doru!Eehh?
>
>So as I see there are no string functions of any sort, and one should
>use tricks instead.
>Getting the substring position is now clear, I'll have to figure out how to get a substring.Use the SUBSTRING () function. But it has SQL syntax, not Delphi syntax.
substring (whatever from 1 for n). If you're not using Fb 2.1, you'll also need to declare the UDF strlen() to your database.
>But wonder how efficient all this would be. Maybe it would be better toIf you want to process a list in your SP, write a lower-level function that lops all characters off the head of an input string, up to but not including your separator (comma or whatever) into an output variable, then lops off and loses the comma, and returns the lopped-off part and what's left.
>send values one at a time.
create procedure breakapart(
inputstring varchar(99) /* same length as your list input string */,
separator char
)
returns (element varchar(3) /* max length of a list item */,
outputstring varchar(99) /* same length as inputstring */,
lengthremaining smallint)
as
declare variable anychar char;
declare variable counter smallint;
begin
element = '';
counter = 0;
while (anychar <> separator ) do
begin
counter = counter + 1;
anychar = substring(inputstring from 1 for 1);
if (anychar <> separator) then
element = element || anychar;
inputstring = substring (inputstring from (counter + 1));
end
outputstring = inputstring;
lengthremaining = strlen(outputstring);
end ^
Call this from your procedures that need to process a list, as per the following example:
create procedure process_my_list (
aSeparatedList varchar(99),
whatever_else_you_want)
....
as
declare variable anElement varchar(3);
declare ListCurrentLength smallint;
begin
ListCurrentLength = strlen(aSeparatedList);
while (ListCurrentLength > 0) do
begin
execute procedure breakapart (aSeparatedList)
returning_values(:anElement, :aSeparatedList, :ListCurrentLength);
-- DO
-- ALL THE STUFF
-- YOU WANT TO DO
-- WITH
-- THE CURRENT
-- LIST ELEMENT
end
end ^
./heLen