Click here to Skip to main content
Click here to Skip to main content

Tagged as

Generating a Sequence in SQL

, , 8 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Generating a sequence in SQL

Introduction

I came through one of the many interesting SQL questions on one of the forums yesterday. The original poster of the question wanted to get a list of the next 15 dates starting today. And he wanted to do this by using cursors. While you can write a cursor to get the results in this case, cursors are not meant to be used like this. A cursor is used to step through the results of a query that returns multiple results, e.g., rows from a table. One good old way of generating a sequence of next n dates would be to use the WHILE loop in SQL.

    DECLARE @LoopCount INT
    SET @LoopCount = 0
     
    DECLARE @calcDate DateTime
    SET @calcDate = GETDATE() 

    DECLARE @outDate varchar(30) 

    WHILE @LoopCount < 15
    BEGIN
     
         SET @outDate = CONVERT(NVARCHAR, @calcDate, 103) 
         PRINT @outDate
         SET @calcDate = DATEADD(dd, 1, @calcDate) 

         SET @LoopCount = @LoopCount + 1
    END

Generating a Sequence of Dates in SQL

Another better alternative would be to use the Common Table Expressions in SQL Server.

A common table expression (CTE) can be thought of as a temporary result set that is defined within the execution scope of a single SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement. A CTE is similar to a derived table in that it is not stored as an object and lasts only for the duration of the query. Unlike a derived table, a CTE can be self-referencing and can be referenced multiple times in the same query.
    WITH q AS
        (
        SELECT  GETDATE() AS datum
        UNION ALL
        SELECT  datum + 1
        FROM    q
        WHERE datum + 1 < GETDATE() + 15
        )
    SELECT  CONVERT(NVARCHAR, DATEADD(dd, DATEDIFF(dd, 0, datum), 0), 103) AS MyDate
    FROM    q

Generating a Sequence of Numbers in SQL

Similarly, you can generate the sequence of numbers in SQL using the Common Table Expressions.

    WITH q AS
        (
        SELECT  1 AS num
        UNION ALL
        SELECT  num + 1
        FROM    q
        WHERE num < 15
        )
    SELECT  * FROM    q

Generating a Sequence of Alphabets in SQL

Or, you can even generate the sequence of alphabets in SQL using the Common Table Expressions.

    WITH q AS
        (
        SELECT  0 AS num
        UNION ALL
        SELECT  num + 1
        FROM    q
        WHERE num <= 26
        )
        SELECT CHAR(num + 65) FROM    q

License

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

Share

About the Authors

Manas Bhardwaj
Architect
Netherlands Netherlands
Read my personal blog at www.manasbhardwaj.net.
Follow on   Twitter

CHill60
Software Developer (Senior)
United Kingdom United Kingdom
Works as a Contract Developer mainly in the Financial Services sector.
 
"Abandon all hope of ever being understood, all ye who have a good idea but don't say it perfectly." Doug Bernard

Comments and Discussions

 
Suggestionan better alternate PinmemberThava Rajan14-May-14 4:15 
GeneralRe: an better alternate PinprotectorCHill(Hawkeye)6015-May-14 6:53 
GeneralRe: an better alternate PinmemberThava Rajan16-May-14 3:17 
QuestionThank you! PinprotectorCHill(Hawkeye)609-May-14 1:24 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 8 May 2014
Article Copyright 2014 by Manas Bhardwaj, CHill60
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid