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

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to find months falls between two dates

Author  Topic 

urzsuresh
Starting Member

30 Posts

Posted - 2010-09-30 : 08:35:38
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

30421 Posts

Posted - 2010-09-30 : 08:40:36
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
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2010-09-30 : 12:45:50
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
   

- Advertisement -