x

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, 2009 at 03:22 PM in Default

BI DWH BALA gravatar image

BI DWH BALA
606 41 60 62

(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, 2009 at 06:25 PM

BI DWH BALA gravatar image

BI DWH BALA
606 41 60 62

(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, 2010 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, 2010 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, 2010 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.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

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

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x378

asked: Nov 27, 2009 at 03:22 PM

Seen: 2132 times

Last Updated: Nov 30, 2009 at 11:03 AM