File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply 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: 8261

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: 26173
    
  66

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
 
Similar Threads
msyql jdbc problem.
Store Password Field in Encryption Form Using MySQL and Hibernate
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException
to_number function question.
displaying the most highly repeated entry in an ArrayList