sql server DATEDIFF
The DATEDIFF function in SQL Server is used to calculate the difference between two dates or times.
The syntax for DATEDIFF is as follows:
DATEDIFF(datepart, startdate, enddate)
-
datepart: Specifies the part of the date or time to calculate the difference. This can be one of the following values:
- 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)
-
startdate: The starting date or time.
-
enddate: The ending date or time.
Example usage:
-- Calculate the difference in days between two dates SELECT DATEDIFF(day, '2021-01-01', '2021-01-10') AS DaysDiff;
-- Calculate the difference in months between two dates SELECT DATEDIFF(month, '2021-01-01', '2021-12-31') AS MonthsDiff;
-- Calculate the difference in hours between two times SELECT DATEDIFF(hour, '09:00:00', '17:00:00') AS HoursDiff;
-- Calculate the difference in minutes between two times SELECT DATEDIFF(minute, '09:00:00', '09:30:00') AS MinutesDiff;
-- Calculate the difference in seconds between two times SELECT DATEDIFF(second, '09:00:00', '09:00:10') AS SecondsDiff
原文地址: https://www.cveoy.top/t/topic/iYJJ 著作权归作者所有。请勿转载和采集!