Subject Re: [firebird-support] counting number of occurances of a sub string in a field
Author Ivan Prenosil
From: "Rajesh Punjabi" <solutions@...>
> Is there a direct way in sql to count the number of occurances of a
> substring inside a field (memo) using fb 1.0.2 without using UDF (user
> defined functions)

If you can use SP and blob is not segmented try:

CREATE TABLE tab ( id INTEGER, b BLOB SUB_TYPE TEXT);

CREATE PROCEDURE proc (id INTEGER, str VARCHAR(100))
RETURNS (num INTEGER)
AS
DECLARE VARIABLE tmp VARCHAR(32000);
BEGIN
tmp = '%' || :str || '%';
num = 0;
WHILE ( EXISTS(SELECT * FROM tab WHERE id=:id AND b LIKE :tmp) ) DO BEGIN
num = num+1;
tmp = tmp || str || '%';
END
END

INSERT INTO tab(id,b) VALUES (1, 'AbcAbcAbcwwwAbcwwwAbc');

EXECUTE PROCEDURE proc 1, 'Abc';
-> 5


Ivan