Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Second Occurance Of a String1 in String2

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: Second Occurance Of a String1 in String2
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

Global Moderator


Joined: 14 Mar 2007
Posts: 10210
Location: italy

PostPosted: Tue Jun 03, 2008 3:19 pm    Post subject: Reply to: Second Occurance Of a String1 in String2
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Retrieving unique occurance Shweta12j DB2 7 Thu Aug 08, 2013 7:57 pm
No new posts Syncsort to copy the last occurance o... abhi.5873 SYNCSORT 8 Mon Mar 25, 2013 11:06 am
No new posts write the last occurance of a record ... abhi.5873 DFSORT/ICETOOL 3 Fri Mar 22, 2013 1:20 pm
No new posts Column having values <STRING1> Pandora-Box DB2 5 Tue Nov 08, 2011 9:14 pm
No new posts Find the last occurance of a string i... bhagyac CLIST & REXX 8 Fri Dec 10, 2010 1:20 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us