IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Second Occurance Of a String1 in String2


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
jeeva84

New User


Joined: 18 Apr 2008
Posts: 7
Location: India

PostPosted: Tue Jun 03, 2008 3:07 pm
Reply with quote

Eg
How will you find the position of second occurance of letter 'T'
in the string 'tom has a toy"
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Tue Jun 03, 2008 3:19 pm
Reply with quote

using what ????
Back to top
View user's profile Send private message
jeeva84

New User


Joined: 18 Apr 2008
Posts: 7
Location: India

PostPosted: Tue Jun 03, 2008 5:05 pm
Reply with quote

Using any DB2 option like LOCATE etc. I want to get that in a query
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Tue Jun 03, 2008 5:19 pm
Reply with quote

Jeeva,

LOCATE and POSSTR will give you only the first occurence ... you may have to fetch and use INSPECT on the col to get the 2nd occurence ..
Back to top
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Tue Jun 03, 2008 5:53 pm
Reply with quote

ashimer wrote:
Jeeva,

LOCATE and POSSTR will give you only the first occurence ... you may have to fetch and use INSPECT on the col to get the 2nd occurence ..


Code:
SELECT * FROM NAMES WHERE POSSTR(LAST_NAME, 'T') <> 0       
AND LOCATE('T', LAST_NAME, POSSTR(LAST_NAME, 'T') + 1) <> 0;   

This works but may cause an error if the first 'T' is in the last position of the LAST_NAME column (I didn't have a quick way to check that).
You could try adding
Code:
AND POSSTR(LAST_NAME, 'T') + 1 <= LENGTH(LAST_NAME)
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Tue Jun 03, 2008 7:10 pm
Reply with quote

Building upon Craq's suggestion use this query to get the 2nd occurence of T ...

Code:


SELECT LOCATE('T',NAME,LOCATE('T',NAME)+1)
FROM TABLE WHERE NAME = 'TOM HAS A TOY';



Thanks Craq for the idea ... I should have tried a lil research on LOCATE function ...
Back to top
View user's profile Send private message
jeeva84

New User


Joined: 18 Apr 2008
Posts: 7
Location: India

PostPosted: Tue Jun 03, 2008 7:40 pm
Reply with quote

Thanks Ashimer and Craq
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Retrieving unique occurance DB2 7
No new posts Syncsort to copy the last occurance o... SYNCSORT 8
No new posts write the last occurance of a record ... DFSORT/ICETOOL 3
No new posts Column having values <STRING1> DB2 5
No new posts Find the last occurance of a string i... CLIST & REXX 8
Search our Forums:

Back to Top