Echo that
If you want to round 6 month to a year then use mm instead of yy, and do
a real / float / decimal division not integer division and round it off
-----Original Message-----
From: Pepon Tom
[mailto:mssqldba-ezmlmshield-x43756400.[Email address protected]
Sent: February 24, 2006 7:47 AM
To: LazyDBA Discussion
Subject: RE: DATEDIFF - strange behaviour?
Per the 2005 MSDN Library definition, the datepart argument specifies on
which
part of the date to calculate the difference.
Since you are specifying the year as the part of the date to calculate
the
difference, I would say that DATEDIFF is returning information precisely
to its
definition.
It's not a bug, it's a feature...
--tom
-----Original Message-----
From: Carol Green
[mailto:mssqldba-ezmlmshield-x45294032.[Email address protected]
Sent: Friday, February 24, 2006 7:42 AM
To: LazyDBA Discussion
Subject: RE: DATEDIFF - strange behaviour?
Mm yes, but this person would have only worked for the company for 1
day. I would want to show this as NO whole years of service, not 1...
-----Original Message-----
From: Paul Ho
[mailto:mssqldba-ezmlmshield-x8878031.[Email address protected]
Sent: 24 February 2006 12:38
To: LazyDBA Discussion
Subject: RE: DATEDIFF - strange behaviour?
Nothing special, try this
select DATEDIFF(yyyy, '12/31/2005', '01/01/2006')
Still returns 1
Where as when you use integer division plus floor / ceiling, you are
imposing your algorithm
-----Original Message-----
From: Carol Green
[mailto:mssqldba-ezmlmshield-x58602917.[Email address protected]
Sent: February 24, 2006 7:27 AM
To: LazyDBA Discussion
Subject: DATEDIFF - strange behaviour?
Hmm,
I was thinking of using the datediff function to calculate length of
service in years, but I started to notice some strange results:
A: DATEDIFF(yyyy, '06/01/2005', '01/01/2006') = 1
B: DATEDIFF(yyyy, '06/01/2005', '12/31/2006') = 1
C: DATEDIFF(yyyy, '06/01/2005', '01/01/2007') = 2
D: DATEDIFF(yyyy, '12/31/2005', '01/01/2007') = 2
whereas
E: FLOOR(DATEDIFF(dd, '06/01/2005', '01/01/2006')/365) = 0
F: FLOOR(DATEDIFF(dd, '06/01/2005', '12/31/2006')/365) = 1
G: FLOOR(DATEDIFF(dd, '06/01/2005', '01/01/2007')/365) = 1
H: FLOOR(DATEDIFF(dd, '12/31/2005', '01/01/2007')/365) = 1
In example A, the datediff value = 1 despite the person having worked
only 6 months. Example B calculates correctly (18 months service) but
add one more day (example C) and the value goes to 2. Even worse, in
example D, the person has worked only just over 12 months but shows as
having 2 years' service. The DATEDIFF function seems to just be
calculating the number of 1st of Januaries between the dates.
Examples D,E,F and G give me the results the I am looking for but
wouldn't take into account leap years.
Is there a better way to calculate the number of whole years between two
dates?
Carol.
---------------------------------------------------------------------
TO REPLY TO EVERBODY , PLEASE CLICK REPLY-ALL, NOT JUST REPLY Website :
http://www.LazyDBA.com To unsubscribe:
http://www.lazydba.com/unsubscribe.html
This email and any files transmitted within it are private and
confidential.
If you are not the intended recipient, this email and any attachments
within it have been transmitted to you in error.
You are prohibited from using, copying, distributing or otherwise using
the information contained within this email.
Nothing in this email message amounts to a contractual or legal
commitment on the part of Optilan unless confirmed by a communication
signed on behalf of the company.
Any email cannot be guaranteed to be secure, error free or free from
viruses. Although every possible care is taken by Optilan, Optilan does
not accept any liability whatsoever for any loss or damage which may be
caused as a result of the transmission of this message by email.
---------------------------------------------------------------------
TO REPLY TO EVERBODY , PLEASE CLICK REPLY-ALL, NOT JUST REPLY Website :
http://www.LazyDBA.com To unsubscribe:
http://www.lazydba.com/unsubscribe.html
---------------------------------------------------------------------
TO REPLY TO EVERBODY , PLEASE CLICK REPLY-ALL, NOT JUST REPLY Website :
http://www.LazyDBA.com To unsubscribe:
http://www.lazydba.com/unsubscribe.html
---------------------------------------------------------------------
TO REPLY TO EVERBODY , PLEASE CLICK REPLY-ALL, NOT JUST REPLY Website :
http://www.LazyDBA.com To unsubscribe:
http://www.lazydba.com/unsubscribe.html
---------------------------------------------------------------------
TO REPLY TO EVERBODY , PLEASE CLICK REPLY-ALL, NOT JUST REPLY Website :
http://www.LazyDBA.com To unsubscribe:
http://www.lazydba.com/unsubscribe.html
MS Sql Server LazyDBA home page