substrlen

<< substr | FB 2.5 Language Reference | tan >>
<< substr | FB 2.1 Language Reference | tan >>
<< substr | FB 2.0 Language Reference | truncate, i64truncate >>

substrlen

Library: ib_udf

Added in: 1.0

Changed in: 1.5.2, 2.0

Better alternative: Internal function SUBSTRING()

Description

Returns the substring starting at startpos and having length characters (or less, if the end of the string is reached first). Positions are 1-based. If either startpos or length is smaller than 1, an empty string is returned. This function only works correctly with single-byte characters.

Result type: VARCHAR(n)

Syntax

 substrlen (str, startpos, length)

Declaration

 DECLARE EXTERNAL FUNCTION substrlen
   CSTRING(255) NULL, SMALLINT, SMALLINT
   RETURNS CSTRING(255) FREE_IT
   ENTRY_POINT 'IB_UDF_substrlen' MODULE_NAME 'ib_udf'

The above declaration is from the file ib_udf2.sql. The NULL after the argument is an optional addition that became available in Firebird 2. If the argument is declared with the NULL keyword, the engine will pass a NULL argument value unchanged to the function. This leads to a NULL result, which is correct. Without the NULL keyword (your only option in pre-2.0 versions), NULL is passed to the function as an empty string and the result is an empty string as well.

For more information about passing NULLs to UDFs, see the note at the end of this book.

Notes:

  • Depending on how you declare it (see CSTRING note), this function can accept and return strings of up to 32767 characters.
  • Before Firebird 2.0, the result type was CHAR(n).
  • In Firebird 1.5.1 and below, the default declaration used CSTRING(80) instead of CSTRING(255).

Tip: Firebird 1.0 has also implemented the internal SQL function SUBSTRING, effectively rendering substrlen obsolete in the same version in which it was introduced. SUBSTRING also supports multi-byte character sets. In new code, use SUBSTRING.

back to top of page
<< substr | FB 2.5 Language Reference | tan >>
<< substr | FB 2.1 Language Reference | tan >>
<< substr | FB 2.0 Language Reference | truncate, i64truncate >>