aspose file tools*
The moose likes JDBC and the fly likes number of times a substring met in a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "number of times a substring met in a string" Watch "number of times a substring met in a string" New topic
Author

number of times a substring met in a string

Asher Tarnopolski
Ranch Hand

Joined: Jul 28, 2001
Posts: 260
hi,
i wanna run on mysql table a method which will return the
entries where a substring is met a wanted number of times in a varchar type field.
for example, let's say there are 4 entries, which varchar field look like this :
------------------
| a |
------------------
| aaa |
------------------
| abacad |
------------------
| bagafaca |
------------------
i wanna get all lines where "a" is met 3 times, so second and third lines only will be returned.
i didn't find any mysql function which seems to be helpful, may be you have ideas about it?
thanx


Asher Tarnopolski
SCJP,SCWCD
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8908
    
    8

SQL LIKE might get you what you want, but you will probably have to do some extra work to weed out the strings that contain more than the given number of substrings.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

Joe is right about the like. If you use two like statements, you can get what you are looking for. Keep in mind that this query is probably slow.
select * from table where field like "%a%a%a%"
and field not like "%a%a%a%a%"


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: number of times a substring met in a string