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


Get the First and Last Day of all the Months based on @ReportDate


Get the First and Last Day of all the Months based on @ReportDate

Author
Message
sql1411
sql1411
SSC Journeyman
SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)

Group: General Forum Members
Points: 92 Visits: 96
Is there a way to get the First and Last Day of all the months (including current) based on the @ReportDate for current year and previous year?
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16311 Visits: 19066
Could you give sample data and expected results?


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
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)

Group: General Forum Members
Points: 85034 Visits: 41074
sql1411 (10/17/2013)
Is there a way to get the First and Last Day of all the months (including current) based on the @ReportDate for current year and previous year?


Yes.
--===== Create and populate the @ReportDate parameter
-- which could be used as an input to a function
-- or stored procedure.
DECLARE @pReportDate DATETIME;
SELECT @pReportDate = GETDATE();

--===== Produce 2 years of monthly start and end dates
-- to include the current year of @pReportDate
WITH
B1(N) AS (SELECT 1 UNION ALL SELECT 1),
B5(N) AS (SELECT 1 FROM B1 a, B1 b, B1 c, B1 d, B1 e),
cteTally(N) AS (SELECT TOP 24 ROW_NUMBER() OVER (ORDER BY B5.N) FROM B5)
SELECT MonthStart = DATEADD(mm,t.N-1,ca.StartDate)
,MonthEnd = DATEADD(mm,t.N ,ca.StartDate)-1
FROM cteTally t
CROSS APPLY (SELECT DATEADD(yy,DATEDIFF(yy,0,@pReportDate)-1,0)) ca (StartDate)
;




However, to make aggregations easier to include any times in the datetimes of the data for reporting, I'd make the code return the first of the month and the first of the following month.

--===== Create and populate the @ReportDate parameter
-- which could be used as an input to a function
-- or stored procedure.
DECLARE @pReportDate DATETIME;
SELECT @pReportDate = GETDATE();

--===== Produce 2 years of monthly start and next month start dates
-- to include the current year of @pReportDate
WITH
B1(N) AS (SELECT 1 UNION ALL SELECT 1),
B5(N) AS (SELECT 1 FROM B1 a, B1 b, B1 c, B1 d, B1 e),
cteTally(N) AS (SELECT TOP 24 ROW_NUMBER() OVER (ORDER BY B5.N) FROM B5)
SELECT MonthStart = DATEADD(mm,t.N-1,ca.StartDate)
,NextMonthStart = DATEADD(mm,t.N ,ca.StartDate)
FROM cteTally t
CROSS APPLY (SELECT DATEADD(yy,DATEDIFF(yy,0,@pReportDate)-1,0)) ca (StartDate)
;




--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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
sql1411
sql1411
SSC Journeyman
SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)

Group: General Forum Members
Points: 92 Visits: 96
Thank you both for your time to answer my post. Really appreciate it.
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