Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract string before the first "-" it finds and the space prior to the "-"

Status
Not open for further replies.

kernal

Technical User
Feb 27, 2001
415
US
I have a field (i.e. TEST 100-001 Summer 2019) and I want to extract 100 from it.

All help is appreciated!
 
Try
Code:
SELECT SUBSTR(myfield,1,INSTR(myfield,'-')-1) AS prefix FROM my_table;
 
To clarify, your field contains:[tt][blue]
TEST 100-001 Summer 2019[/blue][/tt]
and you want to get [tt][blue]100[/blue][/tt] out of it?

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Andy - I was not sure of the requirements myself. By "extract" do we mean "retrieve" or "remove". The previous query would cover the "retrieve" portion. The following should cover "remove":
Code:
SELECT SUBSTR(myfield,INSTR(myfield,'-')+1,50) AS post_hyphen FROM my_table;
This assumes myfield will not be more than 50 characters long.
 
You don't have to worry about 50 characters limit: :)
Code:
SELECT SUBSTR('TEST 100-001 Summer 2019',INSTR('TEST 100-001 Summer 2019','-')+1) AS post_hyphen FROM dual
you get: [tt]001 Summer 2019[/tt]

I replaced myfield with 'TEST 100-001 Summer 2019'

And you are right - it would be nice to know what kernal has and what he wants to get out of it...

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Oh, sure, Andy - exploit the function's features! [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top