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 2012 Forums
 Transact-SQL (2012)
 Date Diff

Author  Topic 

bashkim_svirca
Starting Member

1 Post

Posted - 2013-03-15 : 12:56:07
Can someone help me with this issue. I am using the below queries to get full month between two dates,
but I am not getting it with the below queries as in both cases are not showing same value of months which should have been 13.

select DATEDIFF(month, '2012-03-05', '2013-04-01')
from test1 where contract_no = 1023 - with this query I am getting 13 months

select DATEDIFF(month, '2012-03-05', '2013-03-28')
from test1 where contract_no = 1023 - with this query I am getting 12 months


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-03-15 : 13:01:36
see

http://www.sqlteam.com/article/datediff-function-demystified

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

UnemployedInOz
Yak Posting Veteran

54 Posts

Posted - 2013-03-15 : 23:33:28
-- this will work most of the time.
declare @Day1 date, @Day2 date

select @Day1 = '2012-03-05',
@Day2 = '2013-04-01'

SELECT @Day2 = DateAdd(dd,Day(@day1) * -1,@Day2)

select DATEDIFF(month, @Day1, @Day2)
Go to Top of Page
   

- Advertisement -