• 0

[MySQL] Is there a function similar to substr_count ?


Question

Hey all,

Does anyone know how I can count the number of times a string or character appears in a certain field in MySQL?

For example..

ABC$DEF$GHIJ

Where the returning result would be 2 for the character '$'.

I'm guessing the SQL would be something similar to.....

SELECT id,content FROM articles COUNT(Something?) AS stringCount

I'm looking for a SQL result with the ID number and string count, make sense? :blush:

I'm flummoxed....

Cheers again,

SilverB. :huh:

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Yeah....

I did look around the manual, mainly at the string functions list, I couldn't see anything there. However, there was something using MATCH but it seems my DB engine does not support it.

Hence, my question as to why maybe a generic function which I am missing.

Now, creating a function sounds interesting, I never knew you could do that, sounds a little involved, but hey, in for a penny....

If somebody does have any further information I'd be most grateful, thanks,

SilverB.

Link to comment
Share on other sites

  • 0
Yeah....

I did look around the manual, mainly at the string functions list, I couldn't see anything there. However, there was something using MATCH but it seems my DB engine does not support it.

Hence, my question as to why maybe a generic function which I am missing.

Now, creating a function sounds interesting, I never knew you could do that, sounds a little involved, but hey, in for a penny....

If somebody does have any further information I'd be most grateful, thanks,

SilverB.

I'm no MySQL guy, but from what I can see, MySQL does not support user defined functions. Your best bet is to just return your data and utilize code to extract the info you want.

Link to comment
Share on other sites

  • 0

not professional, but does the job :

mysql> SELECT LENGTH('a$b$c$')-LENGTH(REPLACE('a$b$c$','$','')) AS test;

+------+

| test |

+------+

| 3 |

+------+

1 row in set (0.00 sec)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.