instr1
The instr1 function is used to calculate the position of substring str2 in string str1.
Similar function: instr. The instr function returns the earliest occurrence index of substr in str. However, instr does not support specifying a starting search position or the occurrence count.
Syntax
instr1(string <str1>, string <str2>[, bigint <start_position>[, bigint <nth_appearance>]])
Parameters
Table 1 Parameter descriptions
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| str1 | 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. |
| str2 | 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. |
| start_position | No | BIGINT | Indicates the character position in str1 to start the search from. The default starting position is the first character position, 1. Specifying a negative number is not supported. |
| nth_appearance | No | BIGINT | Indicates the position of the nth_appearance match of str2 in str1. If nth_appearance is of another type or is less than or equal to 0, an error is returned. |
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.
- If str1, str2, start_position, or nth_appearance is NULL, NULL is returned.
Example Code
Returns 10.
select instr1('Tech on the net', 'h', 5, 1); Returns 2.
select instr1('abc', 'b'); Returns NULL.
select instr1('abc', null); What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot