SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


?? on creating a temp table for a date range


?? on creating a temp table for a date range

Author
Message
jbalbo
jbalbo
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1851 Visits: 688
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
J Livingston SQL
J Livingston SQL
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15345 Visits: 38735
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

Sean Lange
Sean Lange
SSC Guru
SSC Guru (79K reputation)SSC Guru (79K reputation)SSC Guru (79K reputation)SSC Guru (79K reputation)SSC Guru (79K reputation)SSC Guru (79K reputation)SSC Guru (79K reputation)SSC Guru (79K reputation)

Group: General Forum Members
Points: 79882 Visits: 18035
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.

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)
r.mitchell
r.mitchell
SSC-Enthusiastic
SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)

Group: General Forum Members
Points: 194 Visits: 96
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


Luis Cazares
Luis Cazares
SSC Guru
SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)

Group: General Forum Members
Points: 51911 Visits: 20016
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:
Hidden RBAR: Counting with Recursive CTE's


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Sean Lange
Sean Lange
SSC Guru
SSC Guru (79K reputation)SSC Guru (79K reputation)SSC Guru (79K reputation)SSC Guru (79K reputation)SSC Guru (79K reputation)SSC Guru (79K reputation)SSC Guru (79K reputation)SSC Guru (79K reputation)

Group: General Forum Members
Points: 79882 Visits: 18035
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.

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)
r.mitchell
r.mitchell
SSC-Enthusiastic
SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)

Group: General Forum Members
Points: 194 Visits: 96
Hmm. thanks for the good read. Time to change some code around :-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search