INSTR
Description of the illustration instr.gif
The
INSTRfunctions searchstringforsubstring. The function returns an integer indicating the position of the character instringthat is the first character of this occurrence.INSTRcalculates strings using characters as defined by the input character set.INSTRBuses bytes instead of characters.INSTRCuses Unicode complete characters.INSTR2uses UCS2 code points.INSTR4uses UCS4 code points.
positionis an nonzero integer indicating the character ofstringwhere Oracle Database begins the search. Ifpositionis negative, then Oracle counts backward from the end ofstringand then searches backward from the resulting position.
occurrenceis an integer indicating which occurrence ofstringOracle should search for. The value ofoccurrencemust be positive.Both
stringandsubstringcan be any of the datatypesCHAR,VARCHAR2,NCHAR,NVARCHAR2,CLOB, orNCLOB. The value returned is ofNUMBERdatatype.Both
positionandoccurrencemust be of datatypeNUMBER, or any datatype that can be implicitly converted toNUMBER, and must resolve to an integer. The default values of bothpositionandoccurrenceare 1, meaning Oracle begins searching at the first character ofstringfor the first occurrence ofsubstring. The return value is relative to the beginning ofstring, regardless of the value ofposition, and is expressed in characters. If the search is unsuccessful (ifsubstringdoes not appearoccurrencetimes after thepositioncharacter ofstring), then the return value is 0.See Also:
Table 2-10, "Implicit Type Conversion Matrix" for more information on implicit conversionThe following example searches the string
CORPORATEFLOOR, beginning with the third character, for the string "OR". It returns the position inCORPORATEFLOORat which the second occurrence of "OR" begins:SELECT INSTR('CORPORATE FLOOR','OR', 3, 2) "Instring" FROM DUAL; Instring ---------- 14In the next example, Oracle counts backward from the last character to the third character from the end, which is the first
OinFLOOR. Oracle then searches backward for the second occurrence ofOR, and finds that this second occurrence begins with the second character in the search string :SELECT INSTR('CORPORATE FLOOR','OR', -3, 2) "Reversed Instring" FROM DUAL; Reversed Instring ----------------- 2The next example assumes a double-byte database character set.
SELECT INSTRB('CORPORATE FLOOR','OR',5,2) "Instring in bytes" FROM DUAL; Instring in bytes ----------------- 27