sql datediff month

Solutions on MaxInterview for sql datediff month by the best coders in the world

showing results for - "sql datediff month"
Nohlan
24 Oct 2019
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
Mariana
05 Oct 2016
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
Bryan
07 Sep 2019
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
queries leading to this page
datediff for months inclusive t sqlsql datediff in hoursdatediff day sqlsql datediff between two datesdatediff in minutes in sqldiff hours between two dates sql serverdatediff minutes sqlsql datediff ssdate diff minutesdatediffdatediff 1 sql serversql datediff yearsdatediff in sql serverdatediff sql sampledays difference between two dates in sqldatediff hours sqlsql ag datediffdatediff in days in sqltsql return datediff in millisql server datetime datediffsql convert datediff to hours and minutesdate time difference between two dates in minutes in sql serverduration in sqlshow datediff in hours and minutes with 2 digits each one sql serverhow to calculate date and year in sqlselect datediff datetime datediff in select statementtotal time difference in mssqldate difference mssqldatediff millisecondsho wto find date difference in sql server without using datediff date date difference in minutes sql servertsql 2c datediffdifference between sql datedifftime difference sql in secondsdatediff days in sqlsql query for datediffmsql date diffsql where difference between two dates in hours and minutesdatediff datetime sql serversql difference between dates in dayssql server datedffsql server datediff hours and minutesbetween datediff mssqlsql date difference daysdatabase datediffdate difference of an entire query in sqlhow to get datediff in months in sqldatediff year sqlsql compare datesdatediff sql server exampledatediff hour sqlselect all date in sql using datediff between two datesdatediff sq c3 96sql datediff funtionsql server datediff minutesdifference of date in minutes 3aseconds in sqltimediff in minutes sql serverselect datediffdatediff sqkdatediff format in sql serverdifference of months between two dates sqldatediff between two dates in sql serverhow to query according to datediffsql datediffwhow to do difference with dates in sqldatediff function in sqldatediff sql querysql datetime difference in hoursdatediff sql c2 b4date subtraction in sqlsql datediff hours and minutesdatediff time sqlt sql datediff hoursdatediff sql yearsselect datediff in sql serversql datediff intervalsms sql datediffdatediff between two dates in sqlsql server datediff in minutesdatediff sql decimal hourstwo date difference in hours minutes seconds sqlsql datediff and return daysdatediff sql number dayst sql datediffhow to get month difference in sqldatediff 28 29 sql serversql get date difference from todaywhat type is datediff sql serverjpql datediffdatediff t sqlhow to take difference of two dates in sqldatediff sql functionsql server syntax date diffdate difference in sqlsql get datediffsql datediff minutes between two datesget years between two dates in sqluse datediff in access sql querydatediff sql year month daysql server date datediffsql time difference in minutesdatediff t sql hourdatediff sqlserverget diff time between 2 dates in sql server in minutes and secondssql query datediff 1 monthsget difference in hours between two dates sqldatediff tsqldifference in hours between two dates sqlsql server datediff in selectdatediff mssqlsql query to find month difference between two datesselect datediff 28 sqlsql server datediff hoursdatediff dias c3 bateis sql serversql datediff funcdatediff sql date differencedatedifference sqlsql datediff minutessql server date difference in minutessql server datediff for monthssql server date arithmetic datediffsql datediff monthdatediff for day in sql serversql server difference between two dates in minutesdiff between two dates sqlyeardiff sqldatedifference vs date 3e sql performancedatediff sql hoursql number of months between two datestsql datediffhow to use datediff in sql querydatediff sql server minutessql server datediff dayssql datediff in minutesdatediff in mssqldatediff 28 29 sqlhow to calculate the duration between two dates in sqlsql difference between two dates in hoursdate diffrnce in sqlsql datediff in daysdatediff in days sqlsqlserver datediffmssql datediffsql datediff secondsdatediff in sql server in seconddatediff as hours sqlhow to find datediff in sqlsql datediff monthsusing date diff hours in sql querysql date differenceget datediff in sqlsql datediff daysget month difference from two dates in sql serverdifference in months between two dates sqldatediff sql serversql datediff compare month and yearsql datediff hoursdate diff in sql server in daysql server datediff in secondshow to use datediff in sqldatediff 28date1 2c date2 29sql date difference in minutesdatediff in sqlsql datediff wksql datediff daydatediff minutes sql serverusing datediffdatediff in sql queryget diff hours and minutes between 2 dates sql serverdatediff in sql in minutessql datediff between a date and days after a datedatediff sql sql server get difference between two datesdatediff access sqlsql datediff functiondatediff in sql from tabledatediff getdate sqlwhere with datediff sqldatediff sql from tablehow to query according to datediff in sqlduration in sql w3schoolsdatediff syntax in sqlt sql difference between two dates in secondshow to calculate month difference in sqlsql datediff columnsql datediff between two dates in hours and minutessql difference between two datetimes in hoursdatediff trong sqlms sql datediff 1 monthdate difference sqldatetime difference in hours sqldatediff sqlsql see the outcome of datediffcompare datetime with minutes and seconds with hours and minutes in sqldatediff days sqlsql datetime difference in hours and minutessql condition datedifftime diff between dates sqlt sql datediffdate difference in minutes in sqlsql get date differencesql difference between two dates in hours and minutessql datediff hourdatediff exampledatediff in minutes sqlsql date diff in hourssql command to et the difference between two datessql subtract datetimesql server datediffsql get datediff in minutesdatediff sql monthdatediff oralce sqldatediff minutesdate time diff in hours and mins in sqlminutes between two dates sqldatediff sql minutessql server datediff exampledatediff in sql explainedsql difference between dateswhat is datediff in sqlsql statement datedifftwo date difference in sqlsql datediff month