Skip to content
Advertisement

T-SQL – Count unique characters in a variable

Goal: To count # of distinct characters in a variable the fastest way possible.

I’ve found some posts in regards to distinct characters in a column, grouped by characters, and etc, but not one for this scenario.

Advertisement

Answer

Using NGrams8K as a base, you can change the input parameter to a nvarchar(4000) and tweak the DATALENGTH, making NGramsN4K. Then you can use that to split the string into individual characters and count them:

Altered NGrams8K:

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