Oracle INSTR函数(子字符串查询)语法详解及应用实例
2016-12-26 22:47
786 查看
据说以下这道看似简单的问题可以测试Oracle查询的能力:
问题:
答案:
select c.ci_id,wm_concat(s.stu_name)
from pm_ci c,pm_stu s
where instr(c.stu_ids,s.stu_id)>0
group by ci_id
答案中用了两个目前没用过的函数:wm_concat()和instr()函数。
前者用来连接字符串,后者则用来查询子字符串。于是,在Oracle Database SQL Reference中找了下资料,讲解的非常详细。如果要快速了解该函数的用法,问一下度娘也是个不错的选择。
-------------------------------------------------------------------------------------------------------------------------
语法如下:
instr( string1, string2, start_position,nth_appearance )
注意:
位置索引号从1开始。
如果String2在String1中没有找到,instr函数返回0。
示例:
SELECT instr('syranmo','s') FROM dual; -- 返回 1
SELECT instr('syranmo','ra') FROM dual; -- 返回 3
SELECT instr('syran mo','at',1,2) FROM dual; -- 返回 0
---------------------------------------------------------------------------------------------------------------------------
INSTR
Syntax
Purpose
The INSTR functions search string for substring. The search operation is defined as comparing the substring argument with substrings of string of the same length for
equality until a match is found or there are no more substrings left. Each consecutive compared substring of string begins one character to the right (for forward searches)
or one character to the left (for backward searches) from the first character of the previous compared substring. If a substring that is equal to substring is found, then
the function returns an integer indicating the position of the first character of this substring. If no such substring is found, then the function returns zero.
■ position is an nonzero integer indicating the character of string where Oracle Database begins the search—that is, the position of the first character of the first substring to compare with substring. 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 substring in string Oracle should search for. The value of occurrence must be positive. If occurrence is greater than 1, then the database does not return on the first match but continues comparing consecutive
substrings of string, as described above, until match number occurrence has been found.
INSTR accepts and returns positions in characters as defined by the input character set, with the first character of string having position 1. INSTRB uses bytes instead of
characters. INSTRC uses Unicode complete characters. INSTR2 uses UCS2 code points.
INSTR4 uses UCS4 code points.
string can be any of the data types CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB.
The exceptions are INSTRC, INSTR2, and INSTR4, which do not allow string to be a CLOB or NCLOB.
substring can be any of the data types CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB.
The value returned is of NUMBER data type.
Both position and occurrence must be of data type NUMBER, or any data type 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.
See Also: Table 3–10, " Implicit Type Conversion Matrix" on page 3-40 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
据说以下这道看似简单的问题可以测试Oracle查询的能力:
问题:
答案:
select c.ci_id,wm_concat(s.stu_name)
from pm_ci c,pm_stu s
where instr(c.stu_ids,s.stu_id)>0
group by ci_id
答案中用了两个目前没用过的函数:wm_concat()和instr()函数。
前者用来连接字符串,后者则用来查询子字符串。于是,在Oracle Database SQL Reference中找了下资料,讲解的非常详细。如果要快速了解该函数的用法,问一下度娘也是个不错的选择。
-------------------------------------------------------------------------------------------------------------------------
语法如下:
instr( string1, string2, start_position,nth_appearance )
string1 | 源字符串,要在此字符串中查找。 |
string2 | 要在string1中查找的字符串 。 |
start_position | 代表string1 的哪个位置开始查找。此参数可选,如果省略默认为1. 字符串索引从1开始。如果此参数为正,从左到右开始检索,如果此参数为负,从右到左检索,返回要查找的字符串在源字符串中的开始索引。 |
nth_appearance | 代表要查找第几次出现的string2. 此参数可选,如果省略,默认为 1.如果为负数系统会报错。 |
位置索引号从1开始。
如果String2在String1中没有找到,instr函数返回0。
示例:
SELECT instr('syranmo','s') FROM dual; -- 返回 1
SELECT instr('syranmo','ra') FROM dual; -- 返回 3
SELECT instr('syran mo','at',1,2) FROM dual; -- 返回 0
---------------------------------------------------------------------------------------------------------------------------
INSTR
Syntax
Purpose
The INSTR functions search string for substring. The search operation is defined as comparing the substring argument with substrings of string of the same length for
equality until a match is found or there are no more substrings left. Each consecutive compared substring of string begins one character to the right (for forward searches)
or one character to the left (for backward searches) from the first character of the previous compared substring. If a substring that is equal to substring is found, then
the function returns an integer indicating the position of the first character of this substring. If no such substring is found, then the function returns zero.
■ position is an nonzero integer indicating the character of string where Oracle Database begins the search—that is, the position of the first character of the first substring to compare with substring. 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 substring in string Oracle should search for. The value of occurrence must be positive. If occurrence is greater than 1, then the database does not return on the first match but continues comparing consecutive
substrings of string, as described above, until match number occurrence has been found.
INSTR accepts and returns positions in characters as defined by the input character set, with the first character of string having position 1. INSTRB uses bytes instead of
characters. INSTRC uses Unicode complete characters. INSTR2 uses UCS2 code points.
INSTR4 uses UCS4 code points.
string can be any of the data types CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB.
The exceptions are INSTRC, INSTR2, and INSTR4, which do not allow string to be a CLOB or NCLOB.
substring can be any of the data types CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB.
The value returned is of NUMBER data type.
Both position and occurrence must be of data type NUMBER, or any data type 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.
See Also: Table 3–10, " Implicit Type Conversion Matrix" on page 3-40 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
相关文章推荐
- Oracle FIRST_VALUE函数语法详解及应用实例
- Oracle列转行函数 Listagg() 语法详解及应用实例
- Oracle Pivot函数语法详解及应用实例
- oracle树结构查询----connect by语法详解
- oracle跨库查询dblink的用法实例详解
- Oracle树结构查询——connect by语法详解
- Oracle分页查询的实例详解
- Oracle创建表语句(Create table)语法详解及示例、、 C# 调用Oracle 存储过程返回数据集 实例
- Excel数据分析与业务建模_第四章_匹配函数MATCH(语法详解及应用实例)
- oracle树结构查询----connect by语法详解
- Oracle模糊查询之(3.3从使用函数和sql语法角度来提高模糊查询效率 三)使用Oracle的instr函数与索引配合提高模糊查询的效率
- Excel数据分析与业务建模_第三章_引用函数INDEX(语法详解及应用实例)
- oracle跨库查询dblink的用法实例详解
- Oracle树结构查询――connect by语法详解
- VC++ Datagrid应用实例详解系列(2) – 筛选查询
- oracle树结构查询----connect by语法详解
- Oracle查询优化日期运算实例详解
- 详解:数据库名、实例名、ORACLE_SID、数据库域名、全局数据库名、服务名
- 用select 查询表中所有的字段名 Oracle开发应用
- ORACLE分页查询SQL语法