x
login about faq Site discussion (meta-askssc)

How to extract a substring from a string in reverse?

Hi,

I have a varchar column. The values are separated using a comma (,).

For example, the column values are as follows:

'P1-101-Part1-This is a part'

'P2-101-Part2-This is a part number 2'

'P3-101-Part1.1-This is a sub part for part 1'

I would like to extract the last value from each column. With respect to the above example, the output should be as follows:

'This is a part'

'This is a part number 2'

'This is a sub part for part 1'

I know that we should use substring function to do this, but I am not sure how I can find out the value after '-' in the reverse direction.

Thanks in advance.

more ▼

asked Nov 27 '09 at 03:22 PM in Default

BI DWH BALA gravatar image

BI DWH BALA
606 28 41 53

(comments are locked)
10|1200 characters needed characters left

4 answers: sort voted first

I found the solution for this problem.

I used Substr and Instr functions. If we specificy position of occurrance of '-' as -1, the instr function will calculate the position in reverse direction.

more ▼

answered Nov 29 '09 at 06:25 PM

BI DWH BALA gravatar image

BI DWH BALA
606 28 41 53

(comments are locked)
10|1200 characters needed characters left

select substr('P1-101-Part1-This is a part',instr('P1-101-Part1-This is a part','-',-1)+1) from dual

more ▼

answered May 10 '10 at 04:29 AM

shabbir ahmed gravatar image

shabbir ahmed
11

(comments are locked)
10|1200 characters needed characters left

use regular expressions in oracle 10g and above .. here's the example

with temp as ( select 'P1-101-Part1-This is a part' STR from dual UNION ALL

select 'P2-101-Part2-This is a part number 2' STR from dual UNION ALL

select 'P3-101-Part1.1-This is a sub part for part 1' STR from dual ) SELECT regexp_substr(str,'[^-]+$',1,1) STR from temp

more ▼

answered Nov 02 '10 at 06:35 PM

shankar.swamy28 gravatar image

shankar.swamy28
11

(comments are locked)
10|1200 characters needed characters left

use substr/instr functions for all versions of oracle.. here's the example

with temp as ( select 'P1-101-Part1-This is a part' STR from dual UNION ALL

select 'P2-101-Part2-This is a part number 2' STR from dual UNION ALL

select 'P3-101-Part1.1-This is a sub part for part 1' STR from dual )

select substr(str, instr(str, '-', -1, 1)+1 ) STR from temp

more ▼

answered Nov 02 '10 at 06:37 PM

shankar.swamy28 gravatar image

shankar.swamy28
11

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x359

asked: Nov 27 '09 at 03:22 PM

Seen: 856 times

Last Updated: Nov 30 '09 at 11:03 AM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.