0

I have a column which contains some values like this

PRODUCT CODE
A099  - Mouse Corded Other                  
X001  - Pointing Devices Family - FP&A Only 
W049A - Video Dualcam Other                 
N029  - Joystick PC Other                   
Y089  - Video Other Other                   
P059  - Gaming Wheels - FP&A Only  

I want to fetch string or code before -. Is this possible ?

So the result set would be something like this

A099
X001
W049A
N029
Y089
P059
1
  • Yes, REGEXP_SUBSTR. Commented Mar 27, 2020 at 14:49

2 Answers 2

1

Use can use regexp_replace():

select regexp_replace(product_code, '^([^ ]+) ', '\1')

Or regexp_substr():

select regexp_substr(product_code, '^[^ ]+')
Sign up to request clarification or add additional context in comments.

3 Comments

REGEXP_SUBSTR(PRODUCT_CODE, '[^-]+')
@MuhammadAsim . . . It depends on whether there are spaces and whether you want them in the final result.
I have used TRIM() to remove spaces. it worked fine though.
1

Use REGEXP_SUBSTR:

SELECT REGEXP_SUBSTR(product_code, '^\S+')
FROM yourTable;

But actually we don't even need to resort to regex here, the base string functions will do just as well:

SELECT SUBSTR(product_code, 1, INSTR(product_code, ' ')-1)
FROM yourTable;

In general, if you can avoid invoking a regex engine and instead use a simpler option, this can be the way to go.

Comments

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.