Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Loop back 14 months using SQL query

Posted on 2006-11-30
5
Medium Priority
?
838 Views
Last Modified: 2007-12-19
Hello All,

I need to loop through backwards 14 months (month wise) and get month and year. Lets say its November 2006 so 14 months back would be oct 2006, sep 2006, aug 2006.......dec 2005, nov 2005, oct 2005. I can use the datepart(month,getdate())-14 but the -ve value is not recognized. Is there a simpler way to loop back month wise from given date and then fetch the month and year.

Regards
RSRSM
0
Comment
Question by:rsrsm
  • 2
  • 2
5 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 18044881
SELECT DATEADD(dd, -14, GETDATE())
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 1200 total points
ID: 18044884
 Sorry


SELECT DATEADD(mm, -14, GETDATE())
0
 

Author Comment

by:rsrsm
ID: 18044958
Thats the fastest and the easiest ans...thankx aneeshattingal ...my mind is not at wrk today...thankx a lot..i was cracking this one frm morning :)
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 18044972
Hi rsrsm,

Select DatePart(Month,DateAdd(month,-14,getDate())) ,DatePart(Year,DateAdd(month,-14,getDate()))

Tim Cottee
0
 

Author Comment

by:rsrsm
ID: 18045009
hey Tim, sorry I had already accepted aneeshattingal answer before you had posted....both answers are quick and serve my purpose....

I'm not sure if I can split the points now.

RSRSM
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question