sql - 从SQL中的字符串中删除指定的字符

标签 sql oracle

说我有一个像这样的字符串",LI,PA,LK";我想删除第一个字符,所以它看起来像"LI,PA,LK";
在Java中,我要处理的代码如下所示:

public String returnSubs(String val) {

    int index = val.indexOf(",");       
    String res = val.substring(index+1, val.length());

    return res;
}

我想要在SQL中实现完全相同的功能,并具有以下查询
    select patientID, case when liver is not null then 'LI' else '' end 
         || case when kidney_r is not null then ',KR' else '' end
         || case when kidney_l is not null then ',KL' else ''end
         || case when heart is not null then ',HE' else '' end
         || case when liver_domino is not null then ',LI-Dom' else '' end
         || case when lung_r is not null then ',LungR' else '' end
         || case when pancreas is not null then ',PA' else '' end
         || case when liver_split is not null then ',Lsplit' else '' end
         || case when lung_l is not null then ',LungL' else '' end
         || case when intestine is not null then ',Intestine' else '' end
         into organType                       
from offers
where patientID > 1
; 

另外,我从上面的查询中获得的字符串可能看起来像LI, PA, KL,(注意逗号在末尾,而不是开头)

我看到我可以使用SUBSTRING和/或SQL的INSTR。但是我不确定如何。我正在创建一个程序来处理

谢谢你的帮助

最佳答案

您也可以在这里使用LTRIM:

SELECT
    LTRIM(organTypes, ',') AS col_out
FROM offers;

某些数据库(例如MySQL)提供了类似于CONCAT_WS的函数,这些函数与分隔符连接,同时确保没有将悬挂的分隔符添加到结果输出中。 Oracle没有这个,但是LTRIM在这里应该足够了。

关于sql - 从SQL中的字符串中删除指定的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59071345/

相关文章:

oracle - sqlplus::ORA-01078:处理系统参数失败

.net - 我需要做什么才能让 Mono 应用程序与 Oracle 对话?

sql - 如何将多个查询优化为单个查询

oracle - Oracle中SQL Server APPLY的等效项是什么?

python - 如何在原始查询中将 Oracle 列安全地绑定(bind)到 ORDER BY 到 SQLAlchemy?

oracle - 如何在 Oracle WebCenter Content Server 上启用或禁用调试工具栏

mysql - SQL 获取值,然后计算这些值的总数

sql - Oracle SQL3 3 嵌套表插入问题

mysql - 我想找到SQL中表的计数总和

mysql 按天分组并计数,然后仅过滤每天的最高值