Oracle InStr Functions (Oracle replaces like with InStr) __ static functions

Source: Internet
Author: User
Oracle InStr functions

For the InStr function, we often use this: to find the position of a specified substring from a string. For example:

Sql> Select InStr (' Oracle ', ' or ') position from dual;

POSITION

----------

1

Starts at the first position of the string ' Oracle ', looking backwards for the first occurrence of the substring ' or '.

In fact, InStr has 4 parameters, the format is "InStr (string, substring, startposition, occurrence)". You can implement the following search for substrings:

1. Search substring starting at specified location

2. Specify the location of the substring that appears the first occurrence of the search

3. Search from Back forward

--1. Start the search from the 3rd character

Sql> Select InStr (' Oracleor ', ' or ', 3) position from dual;

POSITION

----------

7

--2. Start with the 1th character and search for the 2nd occurrence of the substring

Sql> Select InStr (' Oracleor ', ' or ', 1, 2) position from dual;

POSITION

----------

7

--3. Start with the last 1th character and search for the 1th occurrence of the substring

Sql> Select InStr (' Oracleor ', ' or ',-1, 1) position from dual;

POSITION

----------

7

--3. Start with the last 1th character and search for the 2nd occurrence of the substring

Sql> Select InStr (' Oracleor ', ' or ',-1, 2) position from dual;

POSITION

----------

1


Oracle replaces like with InStr

There are nearly 1 million of data in the table, many times we have to do string matching, in SQL statements, we usually use like to achieve our search goals. But the actual test shows that the efficiency of like is quite different from the InStr function. Here are some test results:

Sql> Set Timing on
Sql> Select COUNT (*) from T where InStr (title, ' Oracle ') >0;

COUNT (*)
———-
5478

elapsed:00:00:11.04
Sql> Select COUNT (*) from T where the title like '%oracle% ';

COUNT (*)
———-
5478

elapsed:00:00:31.47
Sql> Select COUNT (*) from T where InStr (title, ' Oracle ') = 0;

COUNT (*)
———-
994530

elapsed:00:00:11.31
Sql> Select COUNT (*) from T where title isn't like '%oracle% ';

COUNT (*)
———-
994530

Note:

InStr (Title, ' Oracle ') >0 equivalent to Like

InStr (Title, ' Oracle ') =0 equivalent to not

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.