Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

create date list Expand / Collapse
Author
Message
Posted Tuesday, December 03, 2013 7:15 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, April 14, 2014 8:25 AM
Points: 116, Visits: 402
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.
Post #1519217
Posted Tuesday, December 03, 2013 7:22 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 2:24 PM
Points: 11,990, Visits: 11,007
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1519220
Posted Tuesday, December 03, 2013 7:49 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 4:25 AM
Points: 4,828, Visits: 11,184
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.



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1519235
Posted Tuesday, December 03, 2013 6:20 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 2:24 AM
Points: 3,591, Visits: 5,100
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1519453
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse