I'm using Postgres 9.5. I have a table with a column that records URLs. Sometimes the URL has a query string and sometimes it does not. I would like to extract the URL, minus any query string, so I came up with:
select substring(url, 0, case position('?' in url) when 0 then length(url)+1 else position('?' in url) end)
from article;
This seems a little wordy and I was wondering if there's a more concise way to do this. My table column is of type TEXT
.
Best Answer
You can use
regexp_replace()
to replace everything after the first?
with nothing:The following example:
returns: