在 PostgreSQL 中选择字符串的值
Selecting values of a string in PostgreSQL
我的 PostgreSQL 中有一个包含字符串值的列 table。
字符串如下所示:
'192.168.1.1-mike.landline,192.136.152-sam.phone,192.364.1.0-main-phone'
基于字符串,我如何 select 逗号开始前的最后一个最大值。即 192.364.1.0-main-phone
我试图查找它,但到目前为止没有成功
使用函数regexp_replace().
select regexp_replace('192.168.1.1-mike.landline,192.136.152-sam.phone,192.364.1.0-main-phone', '.*,', '');
regexp_replace
------------------------
192.364.1.0-main-phone
(1 row)
阅读文档中的 POSIX Regular Expressions。
更新。您可以 trim 以逗号结束带有函数 rtrim()
的字符串,例如:
select rtrim('some_string,', ',')
rtrim
-------------
some_string
(1 row)
因此您的查询可能如下所示:
select regexp_replace(rtrim(the_string, ','), '.*,', '');
我的 PostgreSQL 中有一个包含字符串值的列 table。
字符串如下所示:
'192.168.1.1-mike.landline,192.136.152-sam.phone,192.364.1.0-main-phone'
基于字符串,我如何 select 逗号开始前的最后一个最大值。即 192.364.1.0-main-phone
我试图查找它,但到目前为止没有成功
使用函数regexp_replace().
select regexp_replace('192.168.1.1-mike.landline,192.136.152-sam.phone,192.364.1.0-main-phone', '.*,', '');
regexp_replace
------------------------
192.364.1.0-main-phone
(1 row)
阅读文档中的 POSIX Regular Expressions。
更新。您可以 trim 以逗号结束带有函数 rtrim()
的字符串,例如:
select rtrim('some_string,', ',')
rtrim
-------------
some_string
(1 row)
因此您的查询可能如下所示:
select regexp_replace(rtrim(the_string, ','), '.*,', '');