Dav*_*ave 1 postgresql select string substring
我正在使用 Postgres 9.5。我有一个表,其中有一列记录 URL。有时 URL 具有查询字符串,有时则没有。我想提取 URL,减去任何查询字符串,所以我想出了:
select substring(url, 0, case position('?' in url) when 0 then length(url)+1 else position('?' in url) end)
from article;
Run Code Online (Sandbox Code Playgroud)
这看起来有点罗嗦,我想知道是否有更简洁的方法来做到这一点。我的表列是类型TEXT
。
小智 5
您可以使用regexp_replace()
将第一个之后的所有内容替换?
为任何内容:
select regexp_replace(url, '\?.*$', '')
Run Code Online (Sandbox Code Playgroud)
下面的例子:
with data (url) as (
values
('http://foo.bar/some/page'),
('http://foo.bar/some/page?id=42&name=Marvin')
)
select url, regexp_replace(url, '\?.*$', '') as clean_url
from data;
Run Code Online (Sandbox Code Playgroud)
返回:
select regexp_replace(url, '\?.*$', '')
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3544 次 |
最近记录: |