Home » SQL & PL/SQL » SQL & PL/SQL » retreive the first and third segment of a value
retreive the first and third segment of a value [message #2608] Tue, 30 July 2002 21:08 Go to next message
Gopi
Messages: 8
Registered: July 2002
Junior Member
I want to display the first segment of a value. the value is like this 1234:abcd:efgh. I want to retreive the first segment ie. 1234 and the last segment ie. efgh. But some of the values have only one segment ie. only 1234. so if the value has only one segment, I'll have to display the first segment and if it has three segments, then I'll have to display the first and third segment. Can anyone help me out on this.

Regards
Gopi
Re: retreive the first and third segment of a value [message #2609 is a reply to message #2608] Tue, 30 July 2002 22:23 Go to previous message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
Hi,
This might help you out:
select substr('&test',1, decode(instr('&test',':',1,1),0,length('&test'),instr('&test',':',1,1)-1)) First
     , substr('&test', instr('&test',':',1,2)+1,decode(instr('&test',':',1,1),0,0,length('&test')-instr('&test',':',1,2)+1)) Third
  from dual
/

If you ommit the decode statements, the query will return the value of the segment in the Third Segment when the field contains only one segment. I'm sure there are more beautiful solutions, but this was the first I could come up with... .
Anyway, play around with decode, INSTR and SUBSTR...

HTH,
MHE
Previous Topic: fetching the next and previous column value for the current record
Next Topic: Passing parameter from the command line
Goto Forum:
  


Current Time: Fri Apr 26 01:20:44 CDT 2024