Oracle 字符串查询以及拆分函数
select id,SUBSTR(detail,INSTR(detail,'hijk')+5,Instr(detail,';abcd=')-INSTR(detail,'def')-5) from( selecta.id,a.detail,a.file from table_a wherenotexists( select/*+index(bid)*/id ##带上所有查询,注意如果用别名,那么表名也用别名 from table_b b whereb.id=a.id ) ) wherefilelike'%xxxxxx%' 其中有两个函数需要注意一下: 第一是 INSTR可以有四个参数 第一是数据源,第二是要查询的字符串,第三是起始位置,第四是出现次数 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',2) would return 10. (Finds the second occurrence of 'o') INSTR('Welcome to PSOUG.org',3) would return 18. (Finds the third occurrence of 'o') INSTR('Welcome to PSOUG.org',-2,2) would return 10. (Count back 2,then find the 2nd 'o') INSTR 可以控制查询要搜索的字符串,并且返回出现的位置。 要注意的地方
http://psoug.org/definition/INSTR.htm 第二是SUBSTR有三个参数,第一是数据源,第二是起始位置,第三是长度 参考博文: https://docs.oracle.com/cd/B28359_01/olap.111/b28126/dml_functions_2101.htm#OLADM679 SUBSTR functionsThe 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.
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.
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 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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |