Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to find months falls between two dates
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

urzsuresh
Starting Member

India
30 Posts

Posted - 09/30/2010 :  08:35:38  Show Profile  Reply with Quote
Hi,
I need to bring the result ,what r t months falls between two dates.
(i.e)


Eg.1
Declare @FrDate datetime,@ToDate datetime
Set @FrDate ='2010-01-31 17:38:58.577' 
Set @ToDate ='2010-03-01 17:38:58.577'

need Output 
Result
1
2
3

Eg.2 Suppose date is
Declare @FrDate datetime,@ToDate datetime
Set @FrDate ='2010-05-31 17:38:58.577' 
Set @ToDate ='2010-09-01 17:38:58.577'

need Output 
Result
5
6
7
8
9


Can anyone please guide me. how to bring this result


Suri

SwePeso
Patron Saint of Lost Yaks

Sweden
30421 Posts

Posted - 09/30/2010 :  08:40:36  Show Profile  Visit SwePeso's Homepage  Reply with Quote
SELECT * FROM master..spt_values where type = 'p' and number between datepart(month, @frdate) and datepart(month, @todate)



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

Bustaz Kool
Flowing Fount of Yak Knowledge

USA
1834 Posts

Posted - 09/30/2010 :  12:45:50  Show Profile  Reply with Quote
urzsuresh,

What do you want when the date range wraps around to another year (e.g., 2010-09-30 thru 2011-02-02)?

=======================================
In all life one should comfort the afflicted, but verily, also, one should afflict the comfortable, and especially when they are comfortably, contentedly, even happily wrong. -John Kenneth Galbraith, economist (1908-2006)
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000