Oracle 字符串查詢以及拆分函數

select id,SUBSTR(detail,INSTR(detail,'hijk')+5,Instr(detail,';abcd=')-INSTR(detail,'def')-5)
from(
select a.id,a.detail,a.file
from table_a
where not exists(
select/*+index(b id)*/ id  ##帶上全部查詢,注意若是用別名,那麼表名也用別名
from
table_b b
where b.id=a.id
)
)
where file like '%xxxxxx%'

其中有兩個函數須要注意一下:express

第一是 INSTR

能夠有四個參數oracle

第一是數據源,第二是要查詢的字符串,第三是起始位置,第四是出現次數less

INSTR( source_string, substring [, start_position [, occurrance ] ] )
INSTR('Welcome to PSOUG.org', 'o')        would return 5   (Finds the first occurrence of 'o')

INSTR('Welcome to PSOUG.org', 'o', 1, 1)  would return 5.  (Finds the first occurrence of 'o')

INSTR('Welcome to PSOUG.org', 'o', 1, 2)  would return 10. (Finds the second occurrence of 'o')

INSTR('Welcome to PSOUG.org', 'o', 1, 3)  would return 18. (Finds the third occurrence of 'o')

INSTR('Welcome to PSOUG.org', 'o', -2, 2) would return 10. (Count back 2, then find the 2nd 'o')

INSTR 能夠控制查詢要搜索的字符串,而且返回出現的位置。函數

要注意的地方this

  1. Both source_string and substring can be any of the datatypes CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. The value returned is of NUMBER datatype.
     
  2. Both start_position and occurrence must be an integer of datatype NUMBER, or any datatype that can be implicitly converted to NUMBER.
     
  3. The first position in the source_string is 1, not 0. 沒有要查詢的字符串的時候,返回0
  4. The INSTR search is case sensitive.

http://psoug.org/definition/INSTR.htmspa

第二是SUBSTR 

有三個參數,第一是數據源,第二是起始位置,第三是長度code

參考博文:htm

https://docs.oracle.com/cd/B28359_01/olap.111/b28126/dml_functions_2101.htm#OLADM679ci

SUBSTR functions

The SUBSTR functions (SUBSTR, SUBSTRB, SUBSTRC, SUBSTR2, and SUBSTR4) return a portion of string, beginning at a specified position in the string. The functions vary in how they calculate the length of the substring to return.字符串

  • SUBSTR calculates lengths using characters as defined by the input character set.

  • SUBSTRB calculates lengths using bytes.

  • SUBSTRC calculates lengths using Unicode complete characters.

  • SUBSTR2 calculates lengths using UCS2 code points.

  • SUBSTR4 calculates lengths using UCS4 code points.

Return Value

The return value is the same data type as string.

Syntax

{SUBSTR | SUBSTRB | SUBSTRC | SUBSTR2 | SUBSTR4}(char, position [, substring_length ])

Arguments

string

A text expression that is the base string from which the substring is created.

position

The position at which the first character of the returned string begins.

  • When position is 0 (zero), then it is treated as 1.

  • When position is positive, then the function counts from the beginning of string to find the first character.

  • When position is negative, then the function counts backward from the end of string.

substring_length

The length of the returned string. SUBSTR calculates lengths using characters as defined by the input character set. SUBSTRB uses bytes instead of characters. SUBSTRC uses Unicode complete characters. SUBSTR2 uses UCS2 code points. SUBSTR4 uses UCS4 code points.

When you do not specify a value for this argument, then the function returns all characters to the end of string. When you specify a value that is less than 1, the function returns NA.

Examples

Example 8-120 Retrieving a Charachter Substring

The following example returns several specified substrings of "abcdefg".

SHOW SUBSTR('abcdefg',3,4) 
cdef

SHOW SUBSTR('abcdefg',-5,4) 
cdef

Example 8-121 Retrieving a Substring Using Bytes

Assume a double-byte database character set.

SHOW SUBSTRB('abcdefg',5,4.2) 
cd
相關文章
相關標籤/搜索