2

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.

2
  • Redshift or Postgres? Those are two very different DBMS (even though Redshift is based on Postgres) Commented Jun 17, 2019 at 11:09
  • @a_horse_with_no_name it's Redshift, but since I know Redshift is based on Postgres I also included it in a tag. Commented Jun 17, 2019 at 11:36

1 Answer 1

2

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

select regexp_replace(str, '-[^-]+$', '')
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the help. In redshift this query is not valid. When I remove the last quotation marks, I only get the part I want to remove. Can you let me know who can I select the substring without including the part I'm getting in return now.
@PlarentHaxhidauti . . . It should work: docs.aws.amazon.com/redshift/latest/dg/REGEXP_REPLACE.html.
Thank you I made a spelling mistake in my query. It's working:))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.