1-- DOES NOT ACCOUNT FOR LEAP YEARS
2DECLARE @date1 DATETIME, @date2 DATETIME, @result VARCHAR(100);
3DECLARE @years INT, @months INT, @days INT,
4 @hours INT, @minutes INT, @seconds INT, @milliseconds INT;
5
6SET @date1 = '1900-01-01 00:00:00.000'
7SET @date2 = '2018-12-12 07:08:01.123'
8
9SELECT @years = DATEDIFF(yy, @date1, @date2)
10IF DATEADD(yy, -@years, @date2) < @date1
11SELECT @years = @years-1
12SET @date2 = DATEADD(yy, -@years, @date2)
13
14SELECT @months = DATEDIFF(mm, @date1, @date2)
15IF DATEADD(mm, -@months, @date2) < @date1
16SELECT @months=@months-1
17SET @date2= DATEADD(mm, -@months, @date2)
18
19SELECT @days=DATEDIFF(dd, @date1, @date2)
20IF DATEADD(dd, -@days, @date2) < @date1
21SELECT @days=@days-1
22SET @date2= DATEADD(dd, -@days, @date2)
23
24SELECT @hours=DATEDIFF(hh, @date1, @date2)
25IF DATEADD(hh, -@hours, @date2) < @date1
26SELECT @hours=@hours-1
27SET @date2= DATEADD(hh, -@hours, @date2)
28
29SELECT @minutes=DATEDIFF(mi, @date1, @date2)
30IF DATEADD(mi, -@minutes, @date2) < @date1
31SELECT @minutes=@minutes-1
32SET @date2= DATEADD(mi, -@minutes, @date2)
33
34SELECT @seconds=DATEDIFF(s, @date1, @date2)
35IF DATEADD(s, -@seconds, @date2) < @date1
36SELECT @seconds=@seconds-1
37SET @date2= DATEADD(s, -@seconds, @date2)
38
39SELECT @milliseconds=DATEDIFF(ms, @date1, @date2)
40
41SELECT @result= ISNULL(CAST(NULLIF(@years,0) AS VARCHAR(10)) + ' years,','')
42 + ISNULL(' ' + CAST(NULLIF(@months,0) AS VARCHAR(10)) + ' months,','')
43 + ISNULL(' ' + CAST(NULLIF(@days,0) AS VARCHAR(10)) + ' days,','')
44 + ISNULL(' ' + CAST(NULLIF(@hours,0) AS VARCHAR(10)) + ' hours,','')
45 + ISNULL(' ' + CAST(@minutes AS VARCHAR(10)) + ' minutes and','')
46 + ISNULL(' ' + CAST(@seconds AS VARCHAR(10))
47 + CASE
48 WHEN @milliseconds > 0
49 THEN '.' + CAST(@milliseconds AS VARCHAR(10))
50 ELSE ''
51 END
52 + ' seconds','')
53
54SELECT @result
55
1Small change like this can be done
2
3 SELECT EmplID
4 , EmplName
5 , InTime
6 , [TimeOut]
7 , [DateVisited]
8 , CASE WHEN minpart=0
9 THEN CAST(hourpart as nvarchar(200))+':00'
10 ELSE CAST((hourpart-1) as nvarchar(200))+':'+ CAST(minpart as nvarchar(200))END as 'total time'
11 FROM
12 (
13 SELECT EmplID, EmplName, InTime, [TimeOut], [DateVisited],
14 DATEDIFF(Hour,InTime, [TimeOut]) as hourpart,
15 DATEDIFF(minute,InTime, [TimeOut])%60 as minpart
16 from times) source
1CREATE FUNCTION FullMonthsSeparation
2(
3 @DateA DATETIME,
4 @DateB DATETIME
5)
6RETURNS INT
7AS
8BEGIN
9 DECLARE @Result INT
10
11 DECLARE @DateX DATETIME
12 DECLARE @DateY DATETIME
13
14 IF(@DateA < @DateB)
15 BEGIN
16 SET @DateX = @DateA
17 SET @DateY = @DateB
18 END
19 ELSE
20 BEGIN
21 SET @DateX = @DateB
22 SET @DateY = @DateA
23 END
24
25 SET @Result = (
26 SELECT
27 CASE
28 WHEN DATEPART(DAY, @DateX) > DATEPART(DAY, @DateY)
29 THEN DATEDIFF(MONTH, @DateX, @DateY) - 1
30 ELSE DATEDIFF(MONTH, @DateX, @DateY)
31 END
32 )
33
34 RETURN @Result
35END
36GO
37
38SELECT dbo.FullMonthsSeparation('2009-04-16', '2009-05-15') as MonthSep -- =0
39SELECT dbo.FullMonthsSeparation('2009-04-16', '2009-05-16') as MonthSep -- =1
40SELECT dbo.FullMonthsSeparation('2009-04-16', '2009-06-16') as MonthSep -- =2
41