The Oracle InStr function uses

Source: Internet
Author: User

INSTR (source string, target string, starting position, matching ordinal):
In Oracle/plsql, the InStr function returns the position of the string to intercept in the source string. It is retrieved only once, that is, from the beginning of the character to the end of the character.

first, the syntax is as follows:
InStr (string1, string2 [, Start_position [, Nth_appearance]])
1> parameter Analysis:
①string1: The source string to look up in this string.

②string2: The string to find in string1.
③start_position: Represents the location of the string1 to begin the search.

Note: This parameter is optional, if omitted, the default is 1. The string index starts at 1.

If this parameter is positive, the search starts from left to right.

If this parameter is negative, it is retrieved from right to left, returning the starting index of the string to find in the source string.

④nth_appearance: Represents the string2 to find the occurrence of the first few occurrences.

Note: This parameter is optional, and if omitted, the default is 1. If the system is negative, it will give an error.
Note: If String2 is not found in String1, the InStr function returns 0.
2> Example:
SELECT InStr (' Syranmo ', ' s ') from dual; --Return 1
SELECT InStr (' Syranmo ', ' RA ') from dual; --Return 3
SELECT InStr (' Syran Mo ', ' a ', up to) from dual; --Return 0
SELECT InStr (' Syranmo ', ' an ', -1,1) from dual; --Return 4
SELECT InStr (' abc ', ' d ') from dual; --Return 0

Second, other application examples:
1> from the Staff table query work number code is ' a10001′, ' a10002′ employees:
①select code, Name, dept, occupation from the staff WHERE code in (' A10001 ', ' A10002 ');
②select code, Name, dept, occupation from the staff WHERE code = ' A10001 ' OR code = ' A10002 ';
③select Code, name, dept, occupation from the staff WHERE InStr (' a10001,a10002 ', code) >0;
This usage is often used in stored procedures, and the range string is passed as a parameter. If you use ① 's SQL notation to pass the spelled string in direct use, the stored procedure will error. This is the way to find, the incoming spelling string can be used directly.

2> Fuzzy query:
①select code, Name, dept, occupation from the staff WHERE code like '%001% ';

②select Code, name, dept, occupation from the staff WHERE InStr (Code, ' 001 ') > 0;

Oracle InStr functions use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.