November 24, 2010 at 6:32 am
I need a query that should return me list of last 12 months from the date and time of running the query.
For example
If i am runnning the query today it should give me list like:
Nov-2010
Oct-2010
Sep-2010
Aug-2010
Jul-2010
Jun-2010
May-2010
Apr-2010
Mar-2010
Feb-2010
Jan-2010
Dec-2009
Nov-2009
Please suggest a query
November 24, 2010 at 6:40 am
How many months do you want, is it 12 or 13?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
November 24, 2010 at 6:44 am
Please suggest for n no of months.
November 24, 2010 at 6:47 am
gaurav-404321 (11/24/2010)
Please suggest for n no of months.
No problem:
DECLARE @Today DATETIME, @nMonths TINYINT
SET @Today = GETDATE()
SET @nMonths = 12
SELECT MonthEndDate = DATEADD(dd, -1, DATEADD(month, n.n + DATEDIFF(month, 0, @Today),0))
FROM (SELECT TOP(@nMonths) n = ROW_NUMBER() OVER (ORDER BY NAME) FROM master.dbo.syscolumns) n
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
November 24, 2010 at 6:57 am
Thanks for quick reply.
November 24, 2010 at 7:02 am
You're welcome 🙂
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
November 24, 2010 at 7:23 am
I am only extending the result set to match what the op requested...
DECLARE @Today DATETIME
, @nMonths TINYINT
SET @Today = GETDATE()
SET @nMonths = 12
SELECT MonthEndDate = DATEADD(dd, -1, DATEADD(month, n.n + DATEDIFF(month, 0, @Today), 0))
, SUBSTRING(
DATENAME(MONTH, DATEADD(dd, -1, DATEADD(month, n.n + DATEDIFF(month, 0, @Today), 0))), 1, 3)
+ '-'
+ CAST(YEAR(DATEADD(dd, -1, DATEADD(month, n.n + DATEDIFF(month, 0, @Today), 0))) AS VARCHAR(4))
AS EndDateOP
FROM ( SELECT TOP ( @nMonths )n = ROW_NUMBER() OVER ( ORDER BY NAME )
FROM master.dbo.syscolumns ) n
ORDER BY 1 DESC
-- Cory
November 25, 2010 at 9:39 am
Hi, I have tried running this query,
but this is giving me dates starting from 1year plus from today's date till and back to previous 12months. AS it is starting from 2011 year and giving result till Nov,2010.
But it should start from today's year (2010) and move back to previous 12 months like Nov,2009.
It is giving:
Dec-2011
Nov-2011
Oct-2011
Sep-2011
Aug-2011
Jul-2011
Jun-2011
May-2011
Apr-2011
Mar-2011
Feb-2011
Jan-2011
Dec-2010
Nov-2010
It should give result as:
Dec-2010
Nov-2010
Oct-2010
Sep-2010
Aug-2010
Jul-2010
Jun-2010
May-2010
Apr-2010
Mar-2010
Feb-2010
Jan-2010
Dec-2009
Nov-2009
November 25, 2010 at 9:43 am
Which query are you running? If you altered the code, best post it here.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
November 25, 2010 at 9:48 am
I have not altered the code. Its same as suggested by you yesterday:
DECLARE @Today DATETIME, @nMonths TINYINT
SET @Today = GETDATE()
SET @nMonths = 12
SELECT MonthEndDate = DATEADD(dd, -1, DATEADD(month, n.n + DATEDIFF(month, 0, @Today),0))
FROM (SELECT TOP(@nMonths) n = ROW_NUMBER() OVER (ORDER BY NAME) FROM master.dbo.syscolumns) n
November 25, 2010 at 10:05 am
Sorry my bad - this matches your original requirement in your first post.
You would benefit from experimenting with the code and understanding how it works:)
DECLARE @Today DATETIME, @nMonths TINYINT
SET @Today = GETDATE()
SET @nMonths = 13
SELECT MonthEndDate = DATEADD(dd, -1, DATEADD(month, (2-n.n) + DATEDIFF(month, 0, @Today),0))
FROM (SELECT TOP(@nMonths) n = ROW_NUMBER() OVER (ORDER BY NAME) FROM master.dbo.syscolumns) n
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
February 18, 2016 at 7:18 am
This was exactly the solution I was looking for! I appreciate the post, it is nice to be able to search for a solution that works. Thank you:-D
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply