In Oracle, the string between the specified characters is intercepted through substr and instr:

Source: Internet
Author: User
Abstract: During the development project, a problem occurs: the data obtained from the remote server is composed of a regular string, but what you want is not the string, it is a string, So it uses the powerful Oracle functions. My problem is this. I want to extract the string in the red box shown below

Abstract: During the development project, a problem occurs: the data obtained from the remote server is composed of a regular string, but what you want is not the string, it is a string, So it uses the powerful Oracle functions. My problem is this. I want to extract the string in the red box shown below

Abstract: During the development project, a problem occurs: the data obtained from the remote server is composed of a regular string, but what you want is not the string, it is a string, So it uses the powerful Oracle functions. My problem is this. I want to extract the string in the red box shown below, that is to say, extract the string between the first "-" and the second:

I ::



2. Create a data table:

CREATE TABLE test (name VARCHAR2 (32));

3. insert data into the table:
insert into test values('CA-CA1344-A-11OCT141250-D');insert into test values('JD-JD693-A-11OCT141105-D');

Iv. query results:

Method 1:

select substr(name,instr(name,'-',1)+1,instr(name,'-',instr(name,'-',1)+1)-instr(name,'-',1)-1) from test;

Method 2:

select substr(name,instr(name,'-',1,1)+1,instr(name,'-',1,2)-1-instr(name,'-',1,1)) from test;

5. The result is as follows:



6. Next, let's write another method to take the string between the second "-" And the third:

select substr(name,instr(name,'-',2,2)+1,instr(name,'-',2,3)-1-instr(name,'-',2,2)) from test;


Result 7:

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.