我在Postgresql用户表中具有以下用户名。
**username**
test
123test
123test456
test123
test45test
test55test55
因此,我试图分别获取不包含结束数字值和结束数字值的字符串。
我期待以下输出
str num
----------------
test 0
123test 0
123test 456
test 123
test45test 0
test55test 55
如何编写将返回上述结果的Postgresql查询?
您可以使用regexp_replace()
和substring()
:
select
regexp_replace(username, '\d+$', '') str,
coalesce(substring(username from '\d+$'), 0) num
from mytable