好久没写技术博客了。
最近遇到用sql语句截取字符串的问题,例如字段值为某一格式为“A-B-C-D”,或者“A-B-C”
1、截取出第四段字符D,如果为四段式,则截取,否则返回空
说明:先判断有多少个“-”,然后分别处理
select
case when LEN(teststring)-len(REPLACE( teststring ,'-',''))= 3 then
right( teststring ,charindex('-',reverse( teststring) )-1)
else '' end
from dual
2、截取前三段字符A-B-C。
可根据上面的语句的提示完成
select
case when LEN(teststring)-len(REPLACE( teststring ,'-',''))= 3 then
SUBSTRING( teststring ,0,charindex(right( teststring ,charindex('-',reverse( teststring ) )-1), teststring )-1)
else teststring end
from dual
本文介绍了如何使用SQL语句从特定格式的字符串中截取所需部分。针对“A-B-C-D”或“A-B-C”的格式,提供了截取第四段或前三段字符的方法。通过判断连字符的数量来确定字符串的组成部分,并据此进行截取。
5411

被折叠的 条评论
为什么被折叠?



