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

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Date Diff
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

bashkim_svirca
Starting Member

1 Posts

Posted - 03/15/2013 :  12:56:07  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 03/15/2013 :  13:01:36  Show Profile  Reply with Quote
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

Australia
54 Posts

Posted - 03/15/2013 :  23:33:28  Show Profile  Reply with Quote
-- 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)

Edited by - UnemployedInOz on 03/15/2013 23:39:48
Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.14 seconds. Powered By: Snitz Forums 2000