代码之家  ›  专栏  ›  技术社区  ›  Marko Masnikosa

为什么SUBSTR不使用instr在结束索引处停止?

  •  0
  • Marko Masnikosa  · 技术社区  · 2 年前

    我有一个字符串值,我想让它更形象:

    'Course grade for <font color="#FF0000">Student Name</font>'
    

    SELECT SUBSTR(col, instr(col, '>',1,1)+1, instr(col, '<',1,2)) FROM my_table where course_id=1
    

    退货 Student Name</font> 即使 instr('<',1,2) 返回正确的索引。

    非常感谢!

    1 回复  |  直到 2 年前
        1
  •  1
  •   nbk    2 年前

    必须减去字符串的开头,因为第二个参数是长度和 INSTR

    CREATE TABLE my_table (course_id int,col varchar2(200))
    
    INSERT INTO my_table VALUES(1,'Course grade for <font color="#FF0000">Student Name</font>')
    
    INSERT INTO my_table VALUES(1,'Course grade for <font color="#FF0000">abel </font>')
    
    SELECT SUBSTR(col, instr(col, '>',1,1)+1, instr(col, '</',1,1) -  instr(col, '>',1,1) - 1) FROM my_table where course_id=1
    
    | SUBSTR(COL,INSTR(COL,'>',1,1)+1,INSTR(COL,'</',1,1)-INSTR(COL,'>',1,1)-1) |
    | :------------------------------------------------------------------------ |
    | Student Name                                                              |
    | abel                                                                      |
    

    数据库(&L)&燃气轮机;不停摆弄 here