PostgreSQL 定位函数returns 所有记录
PostgreSQL position function returns all records
select substring(bla,position('PMR' in bla),10) from x
我知道如果在字符串中找不到子字符串,那么位置函数将为 return 0。在 db2 中我可以写 substring(bla,locate('PMR',bla),10 ).我怎样才能在 postgresSQL 中实现这个?
我想在 PMR 后显示 10 个字符。
示例:
abcdefgPMR213141565
abcdefgdaaaaaPMR213141563
Result:
213141565
213141563
只需将搜索字符串的 len
添加到字符串的 postion
即可得到结果。试试这个。
select substring('abcdefgPMR213141565',
position('PMR' in 'abcdefgPMR213141565')+char_length('PMR'),10)
结果: 213141565
更新: 未找到搜索字符串时处理
select substring(a,
position('PMR' in a)+
case when position('PMR' in a) =0 then 0
else char_length('PMR') end ,10)
from x
select substring(bla,position('PMR' in bla),10) from x
我知道如果在字符串中找不到子字符串,那么位置函数将为 return 0。在 db2 中我可以写 substring(bla,locate('PMR',bla),10 ).我怎样才能在 postgresSQL 中实现这个?
我想在 PMR 后显示 10 个字符。
示例:
abcdefgPMR213141565
abcdefgdaaaaaPMR213141563
Result:
213141565
213141563
只需将搜索字符串的 len
添加到字符串的 postion
即可得到结果。试试这个。
select substring('abcdefgPMR213141565',
position('PMR' in 'abcdefgPMR213141565')+char_length('PMR'),10)
结果: 213141565
更新: 未找到搜索字符串时处理
select substring(a,
position('PMR' in a)+
case when position('PMR' in a) =0 then 0
else char_length('PMR') end ,10)
from x