Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / SQL
Tip/Trick

How to Find Character Repeated in a String Few Times using CTE?

Rate me:
Please Sign up or sign in to vote.
4.15/5 (4 votes)
9 Dec 2014CPOL1 min read 46.2K   7   6
This tip shows how to use CTE to find character repeated in a string few times.

Introduction

This tip shows how to find single character repeated in a string few times by using Common Table Expressions (CTE).

Input data 2dblarw Output data

Why CTE?

CTE is recursive query, which returns a temporary result set. Using CTE, you're able to get string from field and to work with it as long as is needed.

Sample Query

SQL
--declare variable, type: table
DECLARE @MyTable TABLE (Input NVARCHAR(30))

--insert sample values
INSERT INTO @MyTable (Input)
VALUES('Abracadabra'), ('Hocus Pocus'), ('Korona Kielce Królem'), ('Chamba Wamba'), ('Vinietai'), ('Corozo')

--here CTE begins: 
;WITH CTE AS
(
    --initial query
    SELECT Input, CONVERT(VARCHAR(1),LEFT(Input,1)) AS Letter, RIGHT(Input, LEN(Input)-1) AS Remainder
    FROM @MyTable
    WHERE LEN(Input)>1
    --recursive part
    UNION ALL
    --recursive query
    SELECT Input, CONVERT(VARCHAR(1),LEFT(Remainder,1)) AS Letter, _
        RIGHT(Remainder, LEN(Remainder)-1) AS Remainder
    FROM CTE
    WHERE LEN(Remainder)>0
)
SELECT Input, Letter, ASCII(Letter) AS CharCode, COUNT(Letter) AS CountOfLetter
FROM CTE
GROUP BY Input, Letter, ASCII(Letter)
HAVING COUNT(Letter)>2

A Brief Description of Sample Query...

...in other words: what the above query does?

Initial query gets 3 columns:

  1. Input field as a first result column. This column is not necessary, but has been placed to display input data,
  2. Letter columns is a result of LEFT[^] function. It gets only one character.
  3. Remainder field is a result of RIGHT[^] function. It gets the string from Input field without first letter.

This query returns:

Abracadabra                    A    bracadabra
Hocus Pocus                    H    ocus Pocus
Korona Kielce Królem           K    orona Kielce Królem
Chamba Wamba                   C    hamba Wamba
Vinietai                       V    inietai
Corozo                         C    orozo

The magic is in the recursive part!

Recursive query goes through the result set of initial query(for the first time) and itself till the length of Remainder field is bigger than one.

Based on "first" record: Abracadabra, see how query process through the Remainder string in each loop:

--first loop of first recursive query
Abracadabra    b    racadabra
--second loop
Abracadabra    r    acadabra
--third loop
Abracadabra    a    cadabra
--and so on
Abracadabra    c    adabra
Abracadabra    a    dabra
Abracadabra    d    abra
Abracadabra    a    bra
Abracadabra    b    ra
Abracadabra    r    a
--last loop
Abracadabra    a    

When each character in Input field has been split into rows, you're able to count it by using aggregate functions[^]. See the last SELECT statement.

SQL
SELECT Input, Letter, ASCII(Letter) AS CharCode, COUNT(Letter) AS CountOfLetter
FROM CTE
GROUP BY Input, Letter, ASCII(Letter)
HAVING COUNT(Letter)>2

For further information, please see:

Final Note

I hope you learn something interesting by reading this tip. Feel free to change CTE to your needs.

History

  • 2014-12-09 - Query has been improved
  • 2014-12-06 - Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
Poland Poland
I'm a lawyer, teacher/trainer and author of VBA programming book (only polish version). Programming is my hobby and the source of extra earnings!
  1. C#
  2. SQL
  3. Linq
  4. VB/VBA/VB.NET
  5. XML & XSL
  6. Python
  7. JCL


If you want to contact me... find me on LinkedIn.

Comments and Discussions

 
QuestionHow to find word occurrence in a paragraph Pin
Tridip Bhattacharjee8-Dec-14 21:52
professionalTridip Bhattacharjee8-Dec-14 21:52 
QuestionRe: How to find word occurrence in a paragraph Pin
Maciej Los8-Dec-14 22:34
mveMaciej Los8-Dec-14 22:34 
AnswerRe: How to find word occurrence in a paragraph Pin
Tridip Bhattacharjee9-Dec-14 2:50
professionalTridip Bhattacharjee9-Dec-14 2:50 
AnswerRe: How to find word occurrence in a paragraph Pin
Maciej Los9-Dec-14 3:50
mveMaciej Los9-Dec-14 3:50 
QuestionWhy the second recursions? Pin
Member 104100768-Dec-14 15:23
Member 104100768-Dec-14 15:23 
AnswerRe: Why the second recursions? Pin
Maciej Los8-Dec-14 22:32
mveMaciej Los8-Dec-14 22:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.