如何使用INSTR()切割字符串

时间:2018-04-12 09:48:18

标签: sql oracle

我需要取括号([])中的值: 示例:

[TextValue][WINDOM][Camry]  

我的代码:

 select substr('[TextValue][WINDOM][Camry]',  
           instr('[TextValue][WINDOM][Camry]', '[',1,2), 
           instr('[TextValue][WINDOM][Camry]', ']',1,2) )                            
      from dual    

期望的结果:WINDOM

2 个答案:

答案 0 :(得分:2)

第三个参数是长度:

with s as (select '[TextValue][WINDOM][Camry]' as Text from Dual)  

select substr(substr(s.Text, instr(s.Text, '[', 1, 2) + 1), 1, instr(substr(s.Text, instr(s.Text, '[', 1, 2) + 1), ']') - 1)                           
from s    

答案 1 :(得分:1)

你几乎就在那里,但SUBSTR有签名SUBSTR( string, start, length )

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE test_data ( value ) AS
  SELECT '[TextValue][WINDOM][Camry]' FROM DUAL;

查询1

SELECT SUBSTR(
         value,
         open_bracket2 + 1,
         close_bracket2 - open_bracket2 - 1
       ) AS bracket2
FROM   (
  SELECT value,
         INSTR( value, '[', 1, 2 ) AS open_bracket2,
         INSTR( value, ']', 1, 2 ) AS close_bracket2
  FROM   test_data
)

<强> Results

| BRACKET2 |
|----------|
|   WINDOM |