Thanks for the post
that looks a little complicated for me.
Actually, it's quite straightforward.
Let's use a simple example, using Northwind.
I can't think of anything in Northwind with semicolon characters, so
lets say the challenge is to count the number of letter a's in
employees' last names.
First, we need a Sequence table of integers. Northwind doesn't have
such a base table so we can create a derived table:
SELECT Sequence.seq
FROM (
SELECT (
SELECT COUNT(*)
FROM Customers AS C2
WHERE C1.CustomerID >= C2.CustomerID
) AS seq
FROM Customers AS C1
) AS Sequence;
We can use the Sequence table to parse out each character in
Employees.LastName:
SELECT E1.LastName, Sequence.seq,
MID(E1.LastName, Sequence.seq, 1) AS char_parsed
FROM Employees AS E1, (
SELECT (
SELECT COUNT(*)
FROM Customers AS C2
WHERE C1.CustomerID >= C2.CustomerID
) AS seq
FROM Customers AS C1
) AS Sequence
WHERE Sequence.seq BETWEEN 1 AND LEN(E1.LastName)
ORDER BY E1.LastName, Sequence.seq;
We can add a column to determine whether the parsed character is an
'a':
SELECT E1.LastName, Sequence.seq,
MID(E1.LastName, Sequence.seq, 1) AS char_parsed,
IIF(MID(E1.LastName, Sequence.seq, 1) = 'a', 1, 0) AS is_a
FROM Employees AS E1, (
SELECT (
SELECT COUNT(*)
FROM Customers AS C2
WHERE C1.CustomerID >= C2.CustomerID
) AS seq
FROM Customers AS C1
) AS Sequence
WHERE Sequence.seq BETWEEN 1 AND LEN(E1.LastName)
ORDER BY E1.LastName, Sequence.seq;
Finally, we can GROUP and SUM:
SELECT E1.LastName, SUM(IIF(MID(E1.LastName, Sequence.seq, 1) = 'a', 1,
0)) AS tally_a
FROM Employees AS E1, (
SELECT (
SELECT COUNT(*)
FROM Customers AS C2
WHERE C1.CustomerID >= C2.CustomerID
) AS seq
FROM Customers AS C1
) AS Sequence
WHERE Sequence.seq BETWEEN 1 AND LEN(E1.LastName)
GROUP BY E1.LastName;
HTH,
Jamie.
--