Oracle InStr and like

Source: Internet
Author: User

Original: http://www.cnblogs.com/crazyjava/archive/2012/10/31/2748202.html

InStr (String1,string2[,start_position[,nth_appearence])

String1: To find in this string.

String2: The string to find in string1.

Start_position: The location to look for from string1. Optional, default is 1, positive numbers are retrieved from left to right, negative numbers are retrieved from right to left.

Nth_appearence: Find the first few occurrences of string2. Optional, default is 1, cannot be negative.

Note: If not found, returns 0.

For example:

Select InStr (' ABCD ', ' a ') from dual; --Return 1
Select InStr (' ABCD ', ' C ') from dual; --Return 3
Select InStr (' ABCD ', ' e ') from dual; --Return 0

This function can be used for fuzzy queries and to determine the inclusion relationship:

For example:

①select code, Name, dept, occupation from the staff where InStr (Code, ' 001 ') > 0;

Equivalent to

Select Code, Name, dept, occupation from the staff where code like '%001% ';

②select Ccn,mas_loc from Mas_loc where InStr (' Fh,fhh,fhm ', CCN) >0;

Equivalent to

Select Ccn,mas_loc from Mas_loc where CCN in (' FH ', ' FHH ', ' FHM ');

Another article:

There are nearly 1 million data in the table, many times we want to do string matching, in the SQL statement, we usually use like to achieve the goal of our search. However, the actual test shows that the efficiency of like is quite different from the InStr function.

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

Note:

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

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

Oracle InStr and like

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.