You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Oracle中实现PostgreSQL的SPLIT_PART()功能?

Oracle 替代 PostgreSQL SPLIT_PART 实现字符串拆分查询

在Oracle中没有直接对应的SPLIT_PART函数,我们可以通过SUBSTR与INSTR函数组合来实现相同的字符串拆分逻辑:

核心查询语句

SELECT SUBSTR(totalscore, 1, INSTR(totalscore, '_') - 1) AS highlow,
       SUBSTR(totalscore, INSTR(totalscore, '_') + 1) AS score,
       score_desc
FROM student_score
WHERE totalscore LIKE 'HN%' OR totalscore LIKE 'LN%';

函数逻辑说明

  • 提取highlow(第一段):
    INSTR(totalscore, '_')定位第一个下划线的位置,SUBSTR从字符串起始处截取到下划线的前一位,得到下划线左侧内容。
  • 提取score(第二段):
    从下划线的下一个位置开始截取到字符串末尾,得到下划线右侧内容。

健壮性优化(可选)

如果totalscore存在无下划线的异常情况,可通过CASE语句避免报错:

SELECT CASE WHEN INSTR(totalscore, '_') > 0 
            THEN SUBSTR(totalscore, 1, INSTR(totalscore, '_') - 1) 
            ELSE totalscore END AS highlow,
       CASE WHEN INSTR(totalscore, '_') > 0 
            THEN SUBSTR(totalscore, INSTR(totalscore, '_') + 1) 
            ELSE NULL END AS score,
       score_desc
FROM student_score
WHERE totalscore LIKE 'HN%' OR totalscore LIKE 'LN%';

内容的提问来源于stack exchange,提问作者user19471184

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 02:45:33