Tuesday, February 1, 2011

Cheat Engine Chicken Invaders 3

Calculating date difference in days, months and years in SQL Server

Calculate the difference between two dates in SQL Server is easy using DATEDIFF . For example, to find the difference between '2007-05-07 'and '2007-05-08' in days just enough (1):

 SELECT DATEDIFF (day, '2007-05-07 ', '2007 -05 to 08 '); 

Result: ----------



1 day DATEDIFF used with ingenuity is helpful to make practical calculations (2), for example:

First and last day of Month:

 SELECT DATEADD (mm, DATEDIFF (mm, 0, getdate ()), 0) 

SELECT dateadd (ms, -3, DATEADD (mm, DATEDIFF (m, 0, getdate ()) +1, 0));

Day last year:

 SELECT dateadd (ms, -3, DATEADD (yy, DATEDIFF (yy, 0, getdate ()), 0)) 

First Monday of the month: SELECT DATEADD

 (wk, DATEDIFF (wk, 0, dateadd (dd ,6-datepart (day, getdate ()), getdate ())), 0); 
However

DATEDIFF is not useful or satisfactory when we make the following calculation :

 2009/02/02 - 2009/01/08 = 1 month and 6 days 
 
   
is, given two dates, returning the number of days, months and years that have passed in a text expression.

The answer that I suggest for this problem is a scalar function in SQL Server that should receive two datetime parameters and return a string expression with days, months and years. You should also make an exception of months or days or years if they are equal to 0.

For example, if we invoke the scalar function: (3)

 select dbo.EntreFechasAñoMesDia ('02 / 10/1987 ', '02 / 02/2011') 
 
 Result: 
 - -------- 
 spend 23 years and 4 months 

Validating the calculation of dates in Wolfram of the Moon? How big do you think the sky?

 

Scale

from Brad Goodspeed on Vimeo
.

0 comments:

Post a Comment