How do I create a dates table with every possible date??

  • Hi Guys

    I have a dates table but i just realized that some years are missing from the table

    I need to create a dates table with dates starting from 1990 till 2017

    I had a look at some previous posts but they were too complicated to understand.

    Can you help me please??

    Thanks a tonne

  • I assume you just want the date in the table.

    CREATE TABLE YourDate (DateCol DATETIME)

    DECLARE @StartDate DATETIME,

    @EndDate DATETIME,

    @WorkDate DATETIME

    SET @StartDate = '01/01/1990'

    SET @EndDate = '12/31/2017'

    SET @WorkDate = '01/01/1990'

    WHILE @WorkDate <= @EndDate

    BEGIN

    INSERT INTO YourDate(DateCol) SELECT @WorkDate

    SET @WorkDate = DATEADD(DD, 1, @WorkDate)

    END

  • Nuts,

    I always like to see what people have up their sleeves for date and other utility tables... would you mind generating/posting the CREATE statement for your date table and maybe even posting the first 10 rows from the table?

    Thank you in advance... I'd really appreciate it.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Heh... considering that it's been over two weeks, I guess you do mind.

    Thanks, anyway 😉

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply