Subject Re: [firebird-support] strlen doesn't work
Author Helen Borrie
At 08:11 PM 28/07/2007, you wrote:
>Hi there
>I'm a Firebird newbie (Oracle is my home), so please pardon me if
>this is a silly question. I'm
>trying to get something like this
>
>SELECT MAX(LENGTH(A_VARCHAR_FIELD)) FROM MY_TABLE
>
>I've tried it with strlen but I alway get this error message:
>
>GDS Exception. 335544569. Dynamic SQL Error
>SQL error code = -804
>Function unknown
>STRLEN
>
>Any hints?

It's very simple: Firebird has very few built-in string functions
(although several have been ported into the v.2.1 beta). You need to
use the external function from ib_udf library. This requires
declaring the external function to your database, as e.g.
DECLARE EXTERNAL FUNCTION strlen
CSTRING(n)
RETURNS INTEGER BY VALUE
ENTRY_POINT 'IB_UDF_strlen' MODULE_NAME 'ib_udf';

where n is a number less than 32768.

Look up the file ib_udf.sql in your installation's /UDF subdirectory
(it has all the declarations plus a bit of doc), as you might find
the function substrlen() suits your needs better...

Note that the declared size can be whatever size best suits your
needs (up to the max length of 32768 bytes). You can make multiple
declarations for the same entry_point using different function names
if you want to, e.g.,

DECLARE EXTERNAL FUNCTION strlen1
CSTRING(512)
RETURNS INTEGER BY VALUE
ENTRY_POINT 'IB_UDF_strlen' MODULE_NAME 'ib_udf';

DECLARE EXTERNAL FUNCTION strlen2
CSTRING(2048)
RETURNS INTEGER BY VALUE
ENTRY_POINT 'IB_UDF_strlen' MODULE_NAME 'ib_udf';

..etc...

But if you are using Fb 2.0 or higher, consider using the built-in
functions CHAR_LENGTH / OCTET_LENGTH / BIT_LENGTH instead (depending
on usage requirements).

./heLen