Click here to Skip to main content
15,868,164 members
Articles / Programming Languages / SQL
Alternative
Tip/Trick

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)
4 Oct 2011CPOL 11.2K   1
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)


Written By
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 3:31
professionalReiss5-Oct-11 3:31 

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.