加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Oracle SUBSTR & INSTR Functions

发布时间:2020-12-12 15:06:54 所属栏目:百科 来源:网络整理
导读:点击打开链接 Oracle SUBSTR INSTR Functions Version 11.1 SUBSTR (Substring) Built-in String Function SUBSTR(overload 1) SUBSTR( STR1VARCHAR2CHARACTER SET ANY_CS, POSPLS_INTEGER,-- starting position LENPLS_INTEGER:= 2147483647) -- number of
Oracle SUBSTR & INSTR FunctionsVersion 11.1SUBSTR (Substring) Built-in String FunctionSUBSTR(overload 1)SUBSTR(
STR1VARCHAR2CHARACTER SET ANY_CS,
POSPLS_INTEGER,-- starting position
LENPLS_INTEGER:= 2147483647) -- number of characters
RETURNVARCHAR2CHARACTER SET STR1%CHARSET;
SUBSTR(overload 2)SUBSTR(
STR1CLOBCHARACTER SET ANY_CS,
POSNUMBER,-- starting position
LENNUMBER:= 2147483647) -- number of characters
RETURNCLOBCHARACTER SET STR1%CHARSET;
Substring Beginning Of StringSELECTSUBSTR(<value>,1,<number_of_characters>)
FROMDUAL;
SELECTSUBSTR('Take the first four characters',1,4) FIRST_FOUR
FROMDUAL;
Substring Middle Of StringSELECTSUBSTR(<value>,<starting_position>,<number_of_characters>)
FROMDUAL.
SELECTSUBSTR('Take the first four characters',16,4) MIDDLE_FOUR
FROMDUAL;

Substring End of String
SELECTSUBSTR(<value>,<starting_position>)
FROMDUAL;
SELECTSUBSTR('Take the first four characters',16) SIXTEEN_TO_END
FROMDUAL;

SELECTSUBSTR('Take the first four characters',-4) FINAL_FOUR
FROMDUAL;
Simplified ExamplesExamples in Oracle/PLSQL of using thesubstr()function to extract a substring from a string:

The general syntax for theSUBSTR() function is:

SUBSTR( source_string,start_position,[ length ] )

"source_string" is the original source_string that the substring will be taken from.

"start_position" is the position in the source_string where you want to start extracting characters.The first position in the string is always '1',NOT '0',as in many other languages.

"length" is an optional parameter that specifies how many characters to extract. If this parameter is not used,SUBSTR will return everything from the start_position to the end of the string.

Notes:
If the start_position is specified as "0",substr treats start_position as "1",that is,as the first position in the string.

If the start_position is apositivenumber,then substr starts from thebeginningof the string.

If the start_position is anegativenumber,then substr starts from theend of the string and counts backwards.

If the length is a negative number,then substr will return a NULL value.

Examples:


substr('Dinner starts in one hour.',8,6) will return 'starts'
substr('Dinner starts in one hour.',8) will return 'starts in one hour.'
substr('Dinner starts in one hour.',6) will return 'Dinner'
substr('Dinner starts in one hour.',-4,3) will return 'our'
substr('Dinner starts in one hour.',-9,3) will return 'one'
substr('Dinner starts in one hour.',2) will return 'on'

This function works identically in Oracle 8i,Oracle 9i,Oracle 10g,and Oracle 11g.

INSTR (Instring) Built-in String FunctionINSTR(overload 1)INSTR(
STR1VARCHAR2CHARACTER SET ANY_CS,-- test string
STR2VARCHAR2CHARACTER SET STR1%CHARSET,-- string to locate
POSPLS_INTEGER:= 1,-- position
NTHPOSITIVE:= 1) -- occurrence number
RETURNPLS_INTEGER;
INSTR(overload 2)INSTR(
STR1CLOBCHARACTER SET ANY_CS,-- test string
STR2CLOBCHARACTER SET STR1%CHARSET,-- string to locate
POSINTEGER:= 1,-- position
NTHPOSITIVE:= 1) -- occurrence number
RETURNINTEGER;
Instring For Matching First Value FoundSELECTINSTR(<value>,<value_to_match>,<direction>,<instance>
FROMDUAL;
SELECTINSTR('Take the first four characters','a',1) FOUND_1
FROMDUAL;
Instring If No Matching Second Value FoundSELECTINSTR('Take the first four characters','a',2) FOUND_2
FROMDUAL;
Instring For Multiple
Characters
SELECTINSTR('Take the first four characters','four',1) MCHARS
FROMDUAL;
Reverse Direction SearchSELECTINSTR('Take the first four characters',-1,1) REV_SRCH
FROMDUAL;
Reverse Direction Search Second MatchSELECTINSTR('Take the first four characters',2) REV_TWO
FROMDUAL;
String Parsing By Combining SUBSTR And INSTR Built-in String FunctionsList parsing first value

Take up to the character before the first comma
SELECTSUBSTR('abc,def,ghi',1,INSTR('abc,',1)-1)
FROMDUAL;
List parsing center value

Take the value between the commas
SELECTSUBSTR('abc,1)+1,
INSTR('abc,2)-INSTR('abc,1)-1)
FROMDUAL;List parsing last value

Take the value after the last comma
SELECTSUBSTR('abc,1)+1)
FROMDUAL;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

点击打开链接

    推荐文章
      热点阅读