How to get substring based on a character and starting to read the string from the right

Viewed 47

I have the following values on a column:

DB3-0800-VRET,
DB3-0800-IC,
IB-TZ-850-IB,
O11FS-OB ...

From each value I want to remove the last part after the dash. I need to have the following result:

DB3-0800-VRET -> DB3-0800,
DB3-0800-IC   -> DB3-0800,
O11FS-OB      -> O11FS

I tried to work with the SPLIT_PART function of RedShift but I didn't have any luck. If someone knows a regex to select the part I need I'd be grateful.

1 Answers

In both Postgres and Redshift, you should be able to use regexp_replace():

select regexp_replace(str, '-[^-]+$', '')
Related