June 25, 2013 at 6:58 am
Hi,
Can someone pint me in the direction of where I can see some examples on creating a temp table for a date range?
What I need to do is use SSRS with parameters for a month and year o create report. I then need to choose data within that month and create a matrix report.
Problem I find is some dates don't have data but I still want to show the date as null so I show all dates in the month. I assume I have to create a temp table using a dimdate table?
Thanks
Joe
June 25, 2013 at 7:17 am
search this site for "calendar table"
lots of articles that may help
________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
June 25, 2013 at 7:55 am
And please don't post multiple threads with the same question.
_______________________________________________________________
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/
June 26, 2013 at 4:08 pm
I use a recursive CTE for this usually. In this example it will give you all the days in June. Had to use this when I created a calendar application with recurring entries. Sort of like outlook and needed to match the day of week based on its recurrence schedule. Like every week, 2 weeks, month... etc.
DECLARE @StartDate DATE = '6/1/2013';
WITH dt(MDate) AS (
SELECT @StartDate AS MDate
UNION ALL
SELECT DATEADD(dd, 1, MDate)
FROM dt
WHERE DATEADD(dd, 1, MDate) < DATEADD(mm, 1, @StartDate)
)
SELECT MDate FROM dt
June 26, 2013 at 4:40 pm
r.mitchell (6/26/2013)
I use a recursive CTE for this usually. In this example it will give you all the days in June. Had to use this when I created a calendar application with recurring entries. Sort of like outlook and needed to match the day of week based on its recurrence schedule. Like every week, 2 weeks, month... etc.
DECLARE @StartDate DATE = '6/1/2013';
WITH dt(MDate) AS (
SELECT @StartDate AS MDate
UNION ALL
SELECT DATEADD(dd, 1, MDate)
FROM dt
WHERE DATEADD(dd, 1, MDate) < DATEADD(mm, 1, @StartDate)
)
SELECT MDate FROM dt
Please, avoid this and read the following article:
June 27, 2013 at 7:47 am
And here is an example of doing this without using a recursive cte for counting. This code also will only return data for the specified month even if the @StartDate is not the first of the month. To see what I mean change the start date to June 2nd and run the recursive version. It will return dates in July because it assumes the start date is the first of the month. Now run the one below with the same start date and it will only return 28 rows now.
DECLARE @StartDate DATE = '6/1/2013';
WITH
E1(N) AS (select 1 from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))dt(n)),
E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
cteTally(N) AS
(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E2
)
select DATEADD(day, N - 1, @StartDate)
from cteTally
where N <= DATEDIFF(day, @StartDate, dateadd(month, datediff(month, 0, @StartDate) + 1, 0))
_______________________________________________________________
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/
June 27, 2013 at 11:43 am
Hmm. thanks for the good read. Time to change some code around 🙂
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply