Home Database Oracle How to intercept some characters in a string in oracle

How to intercept some characters in a string in oracle

Feb 14, 2022 pm 12:47 PM
oracle Intercept string

In Oracle, you can use the Instr() and substr() functions to intercept part of the characters in the string. The syntax is "SUBSTR('String to be intercepted', interception starting position, INSTR('Required Intercepted string,'','search starting position','number of occurrences')-1) ".

How to intercept some characters in a string in oracle

The operating environment of this tutorial: Windows 7 system, Oracle 11g version, Dell G3 computer.

oracle intercepts part of the string in the field

Use the Instr() and substr() functions in Oracle:

In Oracle, you can use the instr function to judge a string to determine whether it contains specified characters.

The syntax is:

instr(sourceString,destString,start,appearPosition).   
instr('源字符串' , '目标字符串' ,'开始位置','第几次出现')
Copy after login

where

  • sourceString represents the source string;

  • destString represents The substring to be searched for in the source string;

  • start represents the starting position of the search. This parameter is optional and defaults to 1;

  • appearPosition represents how many times destString appears from the source character. This parameter is also optional and defaults to 1;

If the value of start is a negative number, then it means The search is performed from right to left, but the location data is still calculated from left to right.

The return value is: the position of the found string.

For the instr function, we often use it like this: find the position of the specified substring from a string. For example:

SQL> select instr('yuechaotianyuechao','ao') position from dual;
 POSITION
 ----------
 6
Copy after login

Start searching from the 7th character

SQL> select instr('yuechaotianyuechao','ao', 7) position from dual;
 POSITION
 ----------
 17
Copy after login

Start from the 1st character and search for the second occurrence of the substring

SQL> select instr('yuechaotianyuechao','ao', 1, 2) position from dual;
 POSITION
 ----------
 17
Copy after login

For the instr function, We often use it like this: find the position of a specified substring in a string. For example:

SQL> select instr('abcdefgh','de') position from dual; 
POSITION 
---------- 
4
Copy after login

Starting from 1, d ranks fourth, so 4 is returned

SQL>select instr('abcdefghbc','bc',3) position from dual; 
POSITION 
---------- 
9
Copy after login

The third character starting from the 3rd character is c, so the string starting from 3 Find bc, return 9

Usage of the substr function, get the string with the specified starting position and length in the string, the default is the substring from the starting position to the end.

 substr( string, start_position, [ length ] )       substr('目标字符串',开始位置,长度)
Copy after login

For example:

      substr('This is a test', 6, 2)     would return 'is' 
      substr('This is a test', 6)     would return 'is a test' 
      substr('TechOnTheNet', -3, 3)     would return 'Net' 
      substr('TechOnTheNet', -6, 3)     would return 'The'select substr('Thisisatest', -4, 2) value from dual
Copy after login

SUBSTR() function

1. Purpose: Returns a string from a given character expression or memo field substring.

2. Grammar format:

substr(string string, int a, int b);
substr(string string, int a) ; 
Copy after login

Format 1:

1. string The string to be intercepted

2. a The starting position of the intercepted string (Note: When a is equal to 0 or 1, interception starts from the first bit)

3. b The length of the string to be intercepted

Format 2:

1. string The string that needs to be intercepted

2. a can be understood as intercepting all subsequent strings starting from the a-th character.

oracle intercepts the string

Intercept C3411.907w15 into the string C3411 before the dot SUBSTR ('C3411.907w15', 0, INSTR ('C3411.907w15 , '.', 1, 1) - 1)

SELECT SUBSTR ('C3411.907w15', 0, INSTR ('C3411.907w15, '.', 1, 1) - 1)
  FROM DUAL
Copy after login

Intercept the string 907w15 after the dot from C3411.907w15 SUBSTR ('C3411.907w15', INSTR ('C3411.907w15', '.' , 1, 1) 1)

SELECT SUBSTR ('C3411.907w15', INSTR ('C3411.907w15', '.', 1, 1)+1)
  FROM DUAL
Copy after login

Project application example:

Original string:

Original There are 7 "_" in the field. Now I just want to take out the string after the last "_". How to solve it?

Two steps:

The first step is to find the 7th "_" character through the Instr() function:

select instr('Q room Net Group_Brokerage Division_South China District_Shenzhen Shihua_Sales Department_Nanshan Qianhai District_Qianhai Central Area_Qianhai Fifth Branch','_', 1, 7) fdisplayname from SHENZHENJM1222.B

Or:

select instr('Qfang.com Group_Brokerage Division_South China District_Shenzhen Shihua_Business Department_Nanshan Qianhai District_Qianhai Center Area_Qianhai Wufen Branch', '_', -1, 1) fdisplayname FROM SHENZHENJM1222.B

The two have the same effect. The -1 below means starting from the right Count the starting character, 1 means to get the first "_"

The obtained result is as shown in the figure:

Step 2, use the substr() function to get the required fields:

select substr(fdisplayname,instr(fdisplayname,'_',-1 ,1) 1) fdisplayname from SHENZHENJM1222.B

fdisplayname: Represents the field name, equivalent to metastring

Focus on the introduction 1

1 means starting to intercept the first string after the target string "_"

Let's take a look at the situation of different numbers:

#select substr(

fdisplayname ,instr(fdisplayname,'_',-1,1) 1) fdisplayname from SHENZHENJM1222.B

Let’s take a look at what 1 in instr(fdisplayname,'_',-1,1) means:

# Recommended tutorial: "

Oracle Tutorial

"

The above is the detailed content of How to intercept some characters in a string in oracle. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

How to paginate oracle database How to paginate oracle database Apr 11, 2025 pm 08:42 PM

Oracle database paging uses ROWNUM pseudo-columns or FETCH statements to implement: ROWNUM pseudo-columns are used to filter results by row numbers and are suitable for complex queries. The FETCH statement is used to get the specified number of first rows and is suitable for simple queries.

How to stop oracle database How to stop oracle database Apr 12, 2025 am 06:12 AM

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.

What steps are required to configure CentOS in HDFS What steps are required to configure CentOS in HDFS Apr 14, 2025 pm 06:42 PM

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

How to create oracle dynamic sql How to create oracle dynamic sql Apr 12, 2025 am 06:06 AM

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values ​​to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.

What to do if the oracle log is full What to do if the oracle log is full Apr 12, 2025 am 06:09 AM

When Oracle log files are full, the following solutions can be adopted: 1) Clean old log files; 2) Increase the log file size; 3) Increase the log file group; 4) Set up automatic log management; 5) Reinitialize the database. Before implementing any solution, it is recommended to back up the database to prevent data loss.

See all articles