I have string values (filed name: filename) that follow a certain template:
filename_this_is_called_Plan_A
file_this_is_Plan_A
filename2024_this_is_known_as_Plan_A
While the strings are all a different length, they all end with _Plan_A
, also known as the segment.
How can I trim this in PostgreSQL in order to always trim the string by the third to last underscore? Basically, so my table looks something like this
filename | segment |
---|---|
filename_this_is_called_Plan_A | Plan_A |
file_this_is_Plan_A | Plan_A |
I’ve tried a lot of different things, but have trouble getting that nth character.
select
filename,
right(filename, charindex('_', reverse(filename)) -1),
count(1)from table
group by 1,2
Output will typically be something like ‘A’ by itself when the desired output is Plan_A
Additionally, this can follow many naming conventions… Plan_A is example. There can be _Plan_B
, _Plan_C
, _File_C
, etc.
2
Answers
Looks like your really want the second to last:
Or, if all trailing strings are six characters like your sample suggests, simply:
If your file name is always after the penultimate (second to last) underscore in the string – then you could use something like below. This code will do the job (using just the basic string functions) regardless the length of the specific name and/or sufix and regardless the number of underscores before the name.
See the fiddle here.