sql server query for datediff

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

showing results for - "sql server query for datediff"
Calvin
27 Apr 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
queries leading to this page
sql datediff minutest sql datediffsql date differencesql server datetime datediffdatediff in sql serversql datediff and return dayssqlserver datediffsql datediff hoursdatediff sqlserversql datediff monthsql server date arithmetic datediffdatediff tsqldatediff access sqlsql server get difference between two datesdatediff sql date differencewhat type is datediff sql serverwhat is datediff in sqlsql datediff columndatediff function in sqlms sql datediffyeardiff sqlsql datediffsql datediff minutes between two datesdate diffrnce in sqldifference between sql datediffsql datediff yearssql server datediff in minutesdatediff in sql explainedsql statement datediffhow to query according to datediff in sqldatediff sql server examplewhow to do difference with dates in sqltsql return datediff in millidatediff sql minutesdays difference between two dates in sqldiff between two dates sqlsql get datediffsql server datediff hourshow to use datediff in sql querysql server datediff exampledatediff t sqldatediff dias c3 bateis sql serverdate diff minutesduration in sqlsql server datediff in selectsql number of months between two datesselect datediff 28 sqlsql get date difference from todaydate subtraction in sqldatediff sql serverdatediff between two dates in sqlselect datediffdatediff oralce sqlsql ag datediffsql datediff wksql server datediffsql server datedffsql server datediff for monthsdatediff sql date date difference in minutes sql serverdatediff in select statementsql datediff secondsdatediff datetime sql serverdatediff in sql queryselect datediff in sql serversql get date differencesql datediff between two datesdatediff sql querysql difference between dates in daysdatediff days in sqldatediff getdate sqluse datediff in access sql querydatediff sql c2 b4datediff exampledatediff sql samplehow to calculate date and year in sqlhow to take difference of two dates in sqltsql 2c datediffmssql datediffdatediff sq c3 96t sql datediffdatediff for day in sql serversql datediff functiondate diff in sql server in daydate difference mssqlsql server datediff dayssql condition datediffho wto find date difference in sql server without using datediff sql see the outcome of datediffdatediff in sqldatediff minutes sql serverdatediff 1 sql serverjpql datediffdatediff sql from tablesql datediff funtiondatediff sql server minutesdatediff sqksql datediff daysdatedifference vs date 3e sql performancewhere with datediff sqlsql server date datediffsql subtract datetimedatediff sql yearssql server difference between two dates in minutesdatediff 28date1 2c date2 29using datediffsql server syntax date diffdatediff in sql from tableselect datediff datetime date difference sqldatediff format in sql serverdatediff 28 29 sqldatediffdate difference in sqltsql datediffsql datediff sssql query for datediffdatediff in sql server in seconddatediff 28 29 sql serverbetween datediff mssqltotal time difference in mssqldatediff in mssqldatediff time sqldatediff syntax in sqlhow to query according to datediffdatabase datediffget years between two dates in sqldatediff in minutes sqlhow to calculate the duration between two dates in sqldatediff in sql in minutesdatedifference sqlsql date difference dayssql server datediff minutesmsql date diffdatediff sqlsql difference between datesdatediff sql functionget datediff in sqlsql datediff funchow to use datediff in sqldatediff mssqltime diff between dates sqlsql server datediff in secondsdatediff minutes sqldate difference of an entire query in sqlduration in sql w3schoolssql datediff in minuteshow to find datediff in sqldatediff millisecondssql server query for datediff