If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > no of occurrence of ',' in a string

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-13-07, 07:07
rahul_s80 rahul_s80 is offline
Registered User
 
Join Date: Jul 2006
Location: Pune , India
Posts: 433
no of occurrence of ',' in a string

i want to count no of occurrence of ',' in a string.
Is there any function to get that
unable to search any such function.
__________________
Rahul Singh
Certified DB2 9 DBA / Application Developer
Reply With Quote
  #2 (permalink)  
Old 02-13-07, 07:37
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
does DB2 have a REPLACE function?

i'm too lazy to look it up on the web and i'm guessing you have a manual handy
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-13-07, 08:02
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Rahul,
There is no built in function to count a character in a string. You can write a UDF to do it though.

Andy
Reply With Quote
  #4 (permalink)  
Old 02-13-07, 08:03
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
r937,
Please start a new thead when asking a new question. DB2 does have a REPLACE function. Look in the Reference manual for particulars.

Andy
Reply With Quote
  #5 (permalink)  
Old 02-13-07, 08:38
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
oh please

as a moderator, i am quite familiar with the problem of people posting new questions in some unrelated thread, and i assure you, i would never do that

SELECT LENGTH(yourstring)
- LENGTH(REPLACE(yourstring,',','')) as number_of_commas
FROM ...

see? no UDF needed
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 02-13-07, 08:53
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Quote:
Originally Posted by r937
oh please

as a moderator, i am quite familiar with the problem of people posting new questions in some unrelated thread, and i assure you, i would never do that

SELECT LENGTH(yourstring)
- LENGTH(REPLACE(yourstring,',','')) as number_of_commas
FROM ...

see? no UDF needed
Well excuse me. Your original post looks like an unrelated question. Maybe if you would have posted the above solution along with the question, it would have been more obvious. If this is just a one time request, then no UDF is necessary, but if it needs to be used frequently, I would still make it a UDF.

Andy
Reply With Quote
  #7 (permalink)  
Old 02-13-07, 08:58
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
that would have required me to look up stuff in the manual, and i don't always feel like doing that

and of course i would look pretty silly posting a solution that doesn't work

REPLACE hasn't always been supported in DB2, which is why i asked -- i'm sure you know there are many different versions of DB2, and by asking the original poster to do the work of looking it up, i was simply trying to determine whether my solution had any chance of working

sorry to have caused you such concern
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On