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)
 Calculate the number of months between two dates

Author  Topic 

ams006
Yak Posting Veteran

55 Posts

Posted - 2009-02-24 : 05:22:44
Hi All,

Hope someone can help me out. It's wuite an urgent resolution that I need.
I'm trying to calculate the number of months between two dates in SQL Server 2005.
Does anyone have any ideas?

Thanks in advance.

ams006

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-02-24 : 05:25:45
[code]select
datediff(month,'20080101',getdate())
[/code]
gives 13

Webfred


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Mangal Pardeshi
Posting Yak Master

110 Posts

Posted - 2009-02-24 : 05:26:46
Try datediff function-

DECLARE @d1 DATETIME,@D2 DATETIME
SET @D1 = '20080501'
SET @D2='20090101'
SELECT DATEDIFF(MM,@d1,@d2)

Mangal Pardeshi
http://mangalpardeshi.blogspot.com
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-02-24 : 05:41:21
someone else there to repeat?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

ams006
Yak Posting Veteran

55 Posts

Posted - 2009-02-24 : 05:41:25
Thank you all
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-24 : 10:15:15
this wont give you correct result always. see this

http://www.sqlteam.com/article/datediff-function-demystified
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-02-24 : 11:41:46
Wow - I didn't knew that!

Thanks a lot for that link VISAKH.

Greetings
Webfred


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-24 : 12:27:41
welcome
Go to Top of Page
   

- Advertisement -