SQL Server DATEDIFF Function: Calculate Date and Time Differences - Complete Guide
The DATEDIFF function in SQL Server is used to calculate the difference between two dates or times. \n\nThe syntax for DATEDIFF is as follows: \n\nDATEDIFF(datepart, startdate, enddate) \n\n- datepart: Specifies the part of the date or time to calculate the difference. This can be one of the following values: \n - year (yy, yyyy) \n - quarter (qq, q) \n - month (mm, m) \n - dayofyear (dy, y) \n - day (dd, d) \n - week (wk, ww) \n - hour (hh) \n - minute (mi, n) \n - second (ss, s) \n - millisecond (ms) \n\n- startdate: The starting date or time. \n\n- enddate: The ending date or time. \n\nExample usage: \n\n-- Calculate the difference in days between two dates \nSELECT DATEDIFF(day, '2021-01-01', '2021-01-10') AS DaysDiff; \n\n-- Calculate the difference in months between two dates \nSELECT DATEDIFF(month, '2021-01-01', '2021-12-31') AS MonthsDiff; \n\n-- Calculate the difference in hours between two times \nSELECT DATEDIFF(hour, '09:00:00', '17:00:00') AS HoursDiff; \n\n-- Calculate the difference in minutes between two times \nSELECT DATEDIFF(minute, '09:00:00', '09:30:00') AS MinutesDiff; \n\n-- Calculate the difference in seconds between two times \nSELECT DATEDIFF(second, '09:00:00', '09:00:10') AS SecondsDiff;
原文地址: https://www.cveoy.top/t/topic/qELw 著作权归作者所有。请勿转载和采集!