Updated on 2026-11-06 GMT+08:00

instr

The instr function is used to return the earliest occurrence index of substr in str.

When a parameter is NULL, NULL is returned. When substr does not exist in str, 0 is returned. Note that the index starts from 1.

Similar function: instr1. The instr1 function calculates the position of substring str2 in string str1. The instr1 function supports specifying a starting search position and the occurrence count.

Syntax

instr(string , string ) 

Parameters

Parameter

Mandatory

Type

Description

str

Yes

STRING

The target string to be searched. If the input is of type BIGINT, DOUBLE, DECIMAL, or DATETIME, it is implicitly converted to STRING type before evaluation. Other types will return an error.

substr

Yes

STRING

The substring to be matched. If the input is of type BIGINT, DOUBLE, DECIMAL, or DATETIME, it is implicitly converted to STRING type before evaluation. Other types will return an error.

Return Values

Returns a value of type BIGINT.

  • If str2 is not found in str1, 0 is returned.
  • If str2 is an empty string, it always matches successfully. For example, select instr('abc',''); returns 1.
  • If str1 or str2 is NULL, NULL is returned.

Example Code

  • Calculate the position of character b in the string abc. The command example is as follows.

    Returns 2.

    select instr('abc', 'b');
  • Any input parameter is NULL. The command example is as follows.

    Returns NULL.

    select instr('abc', null)