Click here to Skip to main content
15,120,078 members
Articles / Programming Languages / SQL
Alternative
Tip/Trick
Posted 5 Oct 2011

Tagged as

Stats

10.2K views

Increment the string value letter by letter for every row using SQL server

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
5 Oct 2011CPOL
WITH tempTable(Column1)AS( SELECT 'chaitanya' [Column1] UNION ALL SELEC`T SUBSTRING(Column1,1,LEN(Column1)-1) FROM tempTable WHERE LEN(Column1)>0)SELECT * FROM tempTable ORDER BY LEN(Column1)Result:cchchachaichaitchaitachaitanchaitanychaitanya
WITH tempTable(Column1)
AS
(
   SELECT 'chaitanya' [Column1] UNION ALL SELEC`T SUBSTRING(Column1,1,LEN(Column1)-1) FROM tempTable WHERE LEN(Column1)>0
)
SELECT * FROM tempTable ORDER BY LEN(Column1)


Result:
VB
c
ch
cha
chai
chait
chaita
chaitan
chaitany
chaitanya

License

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

Share

About the Author

Pascal Ganaye
Software Developer (Senior)
France France
I am a French programmer.
These days I spend most of my time with the .NET framework, JavaScript and html.

Comments and Discussions

 
GeneralReason for my 5 - CTE will be more efficient Pin
Reiss5-Oct-11 4:31
professionalReiss5-Oct-11 4:31 
Reason for my 5 - CTE will be more efficient

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.