regexp_substr在值更改时不起作用

时间:2019-06-27 06:32:15

标签: sql oracle

我想要每个学生的最低和最高分数。以下是示例标记, 我希望uc_min等于/之前的内容,而uc_max等于/

之后的内容

下面是我的代码,它不能用于一位数字

select distinct  regexp_substr(marks,'[^/]+',1,1) uc_min, 
        regexp_substr(marks,'[^/]+',4,1)  uc_max
   from std
  where student_id = 'YYY'

预期输出:

Sample marks   Expected uc_min    Expected uc_max
1/100          1                  100
20/30          20                 30
180/730        180                730
20/200         20                 200

2 个答案:

答案 0 :(得分:0)

一个substr/ instr解决方案,假设只有一个"/"

select substr(marks, 1, instr(marks,'/') - 1) as uc_min,
       substr(marks, instr(marks,'/') + 1) as uc_max
from std;

DEMO

答案 1 :(得分:0)

尝试一下:

WITH MARKS (SAMPLE_MARKS) AS 
(SELECT '1/100' FROM DUAL UNION ALL
SELECT '20/30' FROM DUAL UNION ALL
SELECT '180/730' FROM DUAL UNION ALL
SELECT '20/200' FROM DUAL)
SELECT DISTINCT
    SAMPLE_MARKS,
    REGEXP_SUBSTR(SAMPLE_MARKS, '[^/]+', 1, 1) UC_MIN,
    REGEXP_SUBSTR(SAMPLE_MARKS, '[^/]+', 1, 2) UC_MAX -- start point(1) and position(2) was incorrect in your code 
FROM
    MARKS

enter image description here

db<>fiddle demo

干杯!