Skip to content
Advertisement

Count alpha characters in string field in SQL Server

I am using SQL Server Management Studio v17.9.1. I have column in a database table that contains alphanumeric strings. I need to count the letters in the string and produce a table of the aggregate results showing the number of letters by a count of the records which had that number of letters in the column.

I am also trying, if possible, to make the list dynamic so the rows in the aggregated results goes as far as the max number of letters in a string in the table column.

The examples I have found so far look to scan the string character by character which will be very performance intensive and I have also looked at the replace function but it does not look like it works with regex and I would have to replace each letter in the alphabet in turn.

Thanks for any help.

For example: Table1

Aggregated results:

Advertisement

Answer

Just another option using an ad-hoc tally table

Example

Returns

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement