Introduction
This challenge is all about counting the number of occurrence of characters in a string using a SET based query. The task is to list all the characters and count of that character within the given string.
Sample Data
Data
----
12
xx
Expected Results
Data Chars NumberOfOccurance
---- ----- -------------------
12 1 1 appears (1) times
12 2 2 appears (1) times
xx x x appears (2) times
Rules
- Results must be sorted in ascending order of Data and then by Character.
- The output should be in the same way as it has been shown.
Sample Script
Use the following scripts to generate the sample data for this challenge.
DECLARE @T TABLE(Data VARCHAR(MAX))
INSERT INTO @T
SELECT '12' UNION ALL
SELECT 'xx'
SELECT * FROM @T
Restrictions
- The solution should be a single query that starts with a "SELECT" or “;WITH”.
Notes
Tags:Puzzles, TSQL Beginners Challenge, TC, TSQL Beginners Challenge 14