15.00 - INSTR - Teradata Database

Teradata Database SQL Functions, Operators, Expressions, and Predicates

Product
Teradata Database
Release Number
15.00
Content Type
Programming Reference
Publication ID
B035-1145-015K
Language
English (United States)

INSTR

Purpose  

Searches the source_string argument for occurrences of search_string.

Syntax  

where:

 

Syntax element…

Specifies…

TD_SYSFNLIB

the name of the database where the function is located.

source_string

a character string or string expression.

search_string

a string of characters that the function searches for in source_string.

position

that the search will begin at this position in source_string.

If position is not specified, the search starts at the beginning of source_string.

If position is negative, the function counts and searches backwards from the end of source_string. position cannot have a value of zero.

occurrence

which occurrence of search_string to find in source_string.

If occurrence is not specified, the function searches for the first occurrence.

If occurrence is greater than 1, the function searches for additional occurrences beginning with the second character in the previous occurrence. occurrence cannot be zero or a negative value.

ANSI Compliance

This is a Teradata extension to the ANSI SQL:2011 standard.

Invocation

INSTR is an embedded services system function. For information on activating and invoking embedded services functions, see “Embedded Services System Functions” on page 24.

Argument Types and Rules

Expressions passed to this function must have the following data types:

  • source_string = CHAR, VARCHAR, or CLOB
  • search_string = CHAR, VARCHAR, or CLOB
  • position = INTEGER, BIGINT, or NUMBER
  • occurrence = INTEGER, BIGINT, or NUMBER
  • You can also pass arguments with data types that can be converted to the above types using the implicit data type conversion rules that apply to UDFs.

    Note: The UDF implicit type conversion rules are more restrictive than the implicit type conversion rules normally used by Teradata Database. If an argument cannot be converted to the required data type following the UDF implicit conversion rules, it must be explicitly cast.

    For details, see “Compatible Types” in SQL External Routine Programming.

    If a match is found, the function returns the position (starting from 1) in the source_string for the match; otherwise the function returns 0. If any of the input arguments are NULL, the function returns NULL.

    Result Type

    The result data type is NUMBER.

    Example  

    The following query returns the result 20 indicating the position of 'ch' in 'chip'. This is the second occurrence of 'ch' with the search starting from the second character of the source string.

       SELECT INSTR('choose a chocolate chip cookie','ch',2,2); 

    Example  

    The following query returns the result 2, which indicates the position of the first occurrence of 'N' in the source string with the search starting from the beginning of the string.

       SELECT INSTR('INSTR FUNCTION','N');