Tuesday, November 30, 2010

MySQL functions to count occurences of specified char or string

Let’s say our column in named ‘col’ and table is named ‘t2′.
Count occurrences of specified character within a string

SELECT
LENGTH(
REPLACE(col, '.', '@@')
) -
LENGTH(
col
)
FROM t2

Will count number of occurrences of character ‘.’ in column ‘col’
Count occurrences of specified substring within a string

SELECT
ROUND((
LENGTH(
REPLACE(col, 'ab', REPEAT('ab', 2))
) -
LENGTH(
col
)
) / 2)
FROM t2

Will count number of occurrences of substring ‘ab’ in [...]

information database NETGEAR Related News NCR Related Stats NATIONAL SEMICONDUCTOR Sun Microsystems NATIONAL INSTRUMENTS

Source: http://de77.com/mysql/mysql-functions-to-count-occurences-of-specified-char-or-string

No comments:

Post a Comment