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

Count of characters


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

New User


Joined: 25 Mar 2009
Posts: 78
Location: India

PostPosted: Tue Dec 29, 2009 4:05 pm
Reply with quote

Hi,

My requirement is as below:

I have a query which returns the name of employees, I need to change it in such a way that, there should be a column added to the query which returns the no. of charecter "A" in it.

Current result:

Name

Alpha
Beta
Gamma


Expected Results

Name A_count

Alpha 2
Beta 1
Gamma 2

Is this possible?
Back to top
View user's profile Send private message
expat

Global Moderator


Joined: 14 Mar 2007
Posts: 8797
Location: Welsh Wales

PostPosted: Tue Dec 29, 2009 4:10 pm
Reply with quote

Requirement - a serious business requirement.
Are you sure that you should not have posted this in the "Interview Questions" forum.
Back to top
View user's profile Send private message
ajeshrn

New User


Joined: 25 Mar 2009
Posts: 78
Location: India

PostPosted: Tue Dec 29, 2009 4:24 pm
Reply with quote

Expat,

icon_confused.gif icon_confused.gif This not a question asked for any interviews.We are trying to develop an Portal, for which we have encountered such a requirement. Just wanted to make the understandability of my question easy, and hence given such a scenario. icon_eek.gif
Back to top
View user's profile Send private message
Srihari Gonugunta

Active User


Joined: 14 Sep 2007
Posts: 295
Location: Singapore

PostPosted: Tue Dec 29, 2009 5:36 pm
Reply with quote

Ajesh,
You can do it using a recursive query as below.

Code:
WITH CNTCTE (Name, FIND_LAST, REMAINDER) AS
(SELECT Name
,LOCATE('A',UPPER(Name))
,SUBSTR(Name,LOCATE('A',UPPER(Name)) + 1)
FROM YOUR_TABLE
WHERE UPPER(Name) LIKE '%A%'
UNION ALL
SELECT Name
,FIND_LAST + LOCATE('A',UPPER(REMAINDER))
,SUBSTR(REMAINDER,LOCATE('A',UPPER(REMAINDER)) + 1)
FROM CNTCTE
WHERE UPPER(REMAINDER) LIKE '%A%'
)
SELECT Name,COUNT(Name) AS COUNT_A
FROM CNTCTE
GROUP BY Name
ORDER BY Name;
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Tue Dec 29, 2009 7:36 pm
Reply with quote

since You are talking about employee names,
the info is not so volatile to have to recomputed every time.
I would evaluate the possibility of storing it as a column at row update/insert time
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 To get the count of rows for every 1 ... DB2 3
No new posts Substring number between 2 characters... DFSORT/ICETOOL 2
No new posts To find whether record count are true... DFSORT/ICETOOL 6
No new posts Validating record count of a file is ... DFSORT/ICETOOL 13
No new posts Reading dataset in Python - New Line ... All Other Mainframe Topics 22
Search our Forums:

Back to Top