today:
7
yesterday:
158
Total:
1,001,261

SQL INSTR

admin 2016.05.19 14:54 Views : 785

INSTR

Syntax

Description of instr.gif follows
Description of the illustration instr.gif

Purpose

The INSTR functions search string for substring. The function returns an integer indicating the position of the character in string that is the first character of this occurrence. INSTR calculates strings using characters as defined by the input character set. INSTRB uses bytes instead of characters. INSTRC uses Unicode complete characters. INSTR2 uses UCS2 code points. INSTR4 uses UCS4 code points.

  • position is an nonzero integer indicating the character of string where Oracle Database begins the search. If position is negative, then Oracle counts backward from the end of string and then searches backward from the resulting position.

  • occurrence is an integer indicating which occurrence of string Oracle should search for. The value of occurrence must be positive.

Both string and substring can be any of the datatypes CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. The value returned is of NUMBER datatype.

Both position and occurrence must be of datatype NUMBER, or any datatype that can be implicitly converted to NUMBER, and must resolve to an integer. The default values of both position and occurrence are 1, meaning Oracle begins searching at the first character of string for the first occurrence of substring. The return value is relative to the beginning of string, regardless of the value of position, and is expressed in characters. If the search is unsuccessful (if substring does not appear occurrence times after the position character of string), then the return value is 0.

See Also:

Table 2-10, "Implicit Type Conversion Matrix" for more information on implicit conversion

Examples

The following example searches the string CORPORATE FLOOR, beginning with the third character, for the string "OR". It returns the position in CORPORATE FLOOR at which the second occurrence of "OR" begins:

SELECT INSTR('CORPORATE FLOOR','OR', 3, 2)
  "Instring" FROM DUAL;
 
  Instring
----------
        14

In the next example, Oracle counts backward from the last character to the third character from the end, which is the first O in FLOOR. Oracle then searches backward for the second occurrence of OR, 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
-----------------
               2

The 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
No. Subject Author Date Views
126 LAST DAY SQL admin 2024.01.19 114
125 Null Display Value admin 2024.01.10 111
124 영수증 2023 file admin 2024.01.04 222
123 Column Data 일괄 추가 admin 2023.11.24 129
122 Using SQL*Plus to Unlock and Change Passwords admin 2023.06.04 150
121 Reversing Require HTTPS admin 2023.06.04 84
120 ipconfig admin 2023.06.03 67
119 127.0.0.1:8080/apex/ admin 2023.06.03 83
118 CHANGE TABLE NAME admin 2023.05.31 58
117 DATE -> DAY CONVERT SQL admin 2023.05.25 38
116 YEAR EXPRESSIONS admin 2023.05.25 34
115 GRAPH (BAR) SQL SAMPLE admin 2023.05.25 39
114 Monthly SUM admin 2023.05.22 37
113 INNER JOIN admin 2023.05.19 37
112 LOOP TABLE UPDATE admin 2023.05.19 50
111 Global pages admin 2023.05.09 40
110 ORACLE COLLECTION admin 2023.05.08 38
109 FINAL TEST admin 2023.05.08 44
108 GitHub.com/Oracle/APEX. admin 2023.05.04 40
107 TEST admin 2023.05.03 36