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
.
uri
type instead of text, or look at FTS. Separately, if you need often to do that, why not storing the URL already without the query string, or at least creating another field to store it, with proper triggers for update. Other solution: create a VIEW on your table that hardcodes one for all the above manipulation and then just query this VIEW giving you immediately the URL without the query string.