Thunderhashy
Thunderhashy

Reputation: 5321

oracle string manipulation

I have a simple string manipulation to be done in Oracle, but couldnt find a way. Assuming delimter as '-' If input str = 'abc-123-xyz-456' and lines = 1 then output should be 'abc' I get this using

select substr('abc-123-xyz-456',1,instr('abc-123-xyz-456','-')-1) from dual;

If lines = 2, then I need output as abc-123

If lines = 3, then I need output as abc-123-xyz and so on

I am not able to figure this out in an efficient manner. Thanks in advance

Upvotes: 1

Views: 427

Answers (1)

Thunderhashy
Thunderhashy

Reputation: 5321

I found a cool solution:

select substr('abc-123-xyz-456',1,instr('abc-123-xyz-456','-',1,n)-1) 
  from dual;

Where n is the number of lines. Let me know if there is a better way,

Upvotes: 3

Related Questions