meta data for this page
  •  

substr

Library: ib_udf

Changed in: 1.0, 1.5.2, 2.0

Description

Returns a string's substring from startpos to endpos, inclusively. Positions are 1-based. If endpos is past the end of the string, substr returns all the characters from startpos to the end of the string. This function only works correctly with single-byte characters.

Result type: VARCHAR(n)

Syntax (unchanged)

substr (str, startpos, endpos)

Declaration

DECLARE EXTERNAL FUNCTION substr
  CSTRING(255) NULL, SMALLINT, SMALLINT
  RETURNS CSTRING(255) FREE_IT
  ENTRY_POINT 'IB_UDF_substr' 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).
  • In InterBase, substr returned NULL if endpos lay past the end of the string.

Tip: Although the function arguments are slightly different, consider using the internal SQL function SUBSTRING instead, for better compatibility and multi-byte character set support.