create date list

  • Hi

    i have a table with accountid, stockid, startdate, enddate.

    i need to create a table which is a list of dates between the start/end dates.

    not sure where to begin??

    thanks.

  • spin (12/3/2013)


    Hi

    i have a table with accountid, stockid, startdate, enddate.

    i need to create a table which is a list of dates between the start/end dates.

    not sure where to begin??

    thanks.

    You should probably begin by reading the first link in my signature about best practices when posting questions. It is entirely unclear what you are trying to do here.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • spin (12/3/2013)


    Hi

    i have a table with accountid, stockid, startdate, enddate.

    i need to create a table which is a list of dates between the start/end dates.

    not sure where to begin??

    thanks.

    While I agree with Sean, you can almost certainly do whatever it is you want to do by joining to a calendar table. One of thousands of links here[/url].

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • You can also build a calendar table on-the-fly:

    WITH SampleData (accountid, stockid, startdate, enddate) AS

    (

    SELECT 1,2,CAST('2012-01-02' AS DATETIME),CAST('2012-01-31' AS DATETIME)

    UNION ALL SELECT 1,3,'2013-01-31', '2013-05-15'

    ),

    Tally (n) AS

    (

    SELECT 0 UNION ALL

    SELECT TOP (SELECT MAX(DATEDIFF(day, startdate, enddate)) FROM SampleData)

    ROW_NUMBER() OVER (ORDER BY (SELECT NULL))

    FROM sys.all_columns a CROSS JOIN sys.all_columns b

    )

    SELECT *

    FROM SampleData a

    CROSS APPLY

    (

    SELECT DATEADD(day, n, startdate)

    FROM Tally

    WHERE n <= DATEDIFF(day, startdate, enddate)

    ) b ([date])

    ORDER BY accountid, stockid, [date];


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

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

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