说我有一个像这样的字符串",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/