SQL Server DATEDIFF with Variables
SQL Server DATEDIFF with Variables is a SQL function in the Date/Time Functions category. SQL Server: returns the count of datepart boundaries crossed between two dates. The syntax is DATEDIFF(datepart, startdate, enddate). It accepts datepart, startdate, enddate. It returns date difference count. A typical example: SELECT DATEDIFF(DAY, '2026-01-01', '2026-06-06'); -- 156 days SELECT DATEDIFF(MONTH, '2026-01-01', '2026-06-06'); -- 5 months SELECT DATEDIFF(YEAR, '2020-01-01', '2026-06-06'); -- 6 years -- Employee tenure: SELECT name, hire_date, DATEDIFF(DAY, hire_date, GETDATE()) AS days_employed, DATEDIFF(YEAR, hire_date, GETDATE()) AS years_employed FROM employees; -- Note: DATEDIFF counts boundaries, not precise duration -- DATEDIFF(YEAR, '2026-12-31', '2027-01-01') = 1 (year boundary crossed) -- For precise: DATEDIFF_BIG for large values A close relative is NOW(), which returns the current date and time as a timestamp. A close relative is CURDATE(), which returns the current date without time. A close relative is DATE(), which extracts the date part from a datetime expression.