0

I have a data field in an Oracle 12cR1 database. The field is a product descriptor, and the start of the field contains the product ID. It is then followed by the product name. I need to strip off the Product ID. The product ID can be in one of 2 formats... The data typically looks like one of the following:

  • 0P3H - Opus NEM
  • 0P22 - 40021 - Order Capture Self Service

Note that there are some cases where the '-' character is part of the product name, such as

  • 0C34 - HR - Promotions Module

As far as I can tell, I need to delete the following WHEN IT IS AT THE START OF THE STRING...

  • 4 characters then space then dash then space
  • 4 characters then space then dash then space 5 characters space dash space

How would I write Oracle SQL to read a field and REMOVE these 2 different substrings?

Thanks!

1

2 Answers 2

1

From your description, case might be the simplest:

select (case when field like '____ - _____ -%' then substr(field, 15)
             else substr(field, 7)
        end)
Sign up to request clarification or add additional context in comments.

1 Comment

This worked, although I had to add the '%' , like this...select (case when field like '____ - _____ -%' then substr(field, 15) else substr(field, 7)
0

Substring in reverse to the first '-' and then substr to the end of the string from that starting position.

select TRIM(substr('0P3H - Opus NEM', instr('0P3H - Opus NEM', '-', -1, 1) + 1)) from dual

select TRIM(substr('0P22 - 40021 - Order Capture Self Service', instr('0P22 - 40021 - Order Capture Self Service', '-', -1, 1) + 1)) from dual

1 Comment

Sorry, I just noticed that you mentioned that in some cases '-' can be part of the name. In that case, the method I described will not work.

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.