Postgresql-如何从字符串的末尾和没有结束数字的字符串中获取数字值

问题描述 投票:0回答:1

我在Postgresql用户表中具有以下用户名。

**username**
test
123test
123test456
test123
test45test
test55test55

因此,我试图分别获取不包含结束数字值和结束数字值的字符串。

我期待以下输出

str          num
----------------
test         0
123test      0
123test      456
test         123
test45test   0
test55test   55

如何编写将返回上述结果的Postgresql查询?

string postgresql trim numeric
1个回答
0
投票

您可以使用regexp_replace()substring()

select
    regexp_replace(username, '\d+$', '') str,
    coalesce(substring(username from '\d+$'), 0) num
from mytable
© www.soinside.com 2019 - 2024. All rights reserved.