Kontext Copilot - An AI assistant for data analytics. Learn more
Expression of Interest
Calculate time difference in T-SQL / SQL Server
insights Stats
warning Please login first to view stats information.
Raymond
Code Snippets & Tips
Code snippets and tips for various programming languages/frameworks. All code examples are under MIT or Apache 2.0 license unless specified otherwise.
This code snippet shows how to calculate time differences.
In SQL Server, DATEDIFF function can be used to find the differences between two dates/timestamps.
The syntax is:
DATEDIFF ( datepart , startdate , enddate )
For parameter datepart, refer to the following table:
Datepart | Abbrevations |
year | yy,yyyy |
quarter | qq,q |
month | mm,m |
dayofyear | dy,y |
day | dd,d |
week | wk,ww |
hour | hh |
minute | mi,n |
second | ss,s |
millisecond | ms |
microsecond | mcs |
nanosecond | ns |
Code snippet
SELECT DATEDIFF(m,CAST('20171015' AS DATE), CAST('20191015' AS DATE)) AS Diff_Months; SELECT DATEDIFF(d,CAST('20171015' AS DATE), CAST('20191015' AS DATE)) AS Diff_Days; SELECT DATEDIFF(ww,CAST('20171015' AS DATE), CAST('20191015' AS DATE)) AS Diff_Weeks; SELECT DATEDIFF(HH,CAST('20171015' AS DATE), CAST('20191015' AS DATE)) AS Diff_Hours;
info Last modified by Raymond 3 years ago
copyright
This page is subject to Site terms.
comment Comments
No comments yet.