Working on postgres SQL.
I have a table with a column that contains values of the following format:
Set1/Set2/Set3/…
Seti can be a set of values for each i. They are delimited by ‘/’.
I would like to show distinct entries of the form set1/set2 and that is – I would like to trim or truncate the rest of the string in those entries.
That is, I want all distinct options for:
Set1/Set2
A regular expression would work great: I want a substring of the pattern: .*/.*/
to be displayed without the rest of it.
I got as far as:
select distinct column_name from table_name
but I have no idea how to make the trimming itself.
Tried looking in w3schools and other sites as well as searching SQL trim / SQL truncate in google but didn’t find what I’m looking for.
Thanks in advance.
If they really are that regular you could use
substring; for example:There is also a version of
substringthat understands POSIX regular expressions if you need a little more flexibility.The PostgreSQL online documentation is quite good BTW:
and it even has a usable index and sensible navigation.
If you want to use
.*/.*then you’d want something likesubstring(s from '[^/]+/[^/]+'), for example: