Working with dates and times is a fundamental aspect of database development and reporting. T-SQL (Transact-SQL), Microsoft’s extension of SQL, offers a robust set of functions to handle date and time values efficiently. This article presents the most commonly used date functions in T-SQL, organized by category and use case, with syntax, examples, and notes on performance.
1. GETDATE() and SYSDATETIME()
Returns the current system date and time.
Function | Returns | Precision |
---|---|---|
GETDATE() | datetime | 3.33 ms |
SYSDATETIME() | datetime2 | 100 ns |
SELECT GETDATE(); -- e.g., 2025-05-01 14:32:45.340
SELECT SYSDATETIME(); -- e.g., 2025-05-01 14:32:45.3401234
SQLUse SYSDATETIME()
when you need higher precision.
2. DATEPART(), DATENAME(), YEAR(), MONTH(), DAY()
These functions extract components of a date.
Function | Description |
---|---|
DATEPART(part, date) | Returns an integer of the date part. |
DATENAME(part, date) | Returns the name (e.g., Monday). |
YEAR(date) | Extracts the year. |
MONTH(date) | Extracts the month (1-12). |
DAY(date) | Extracts the day of the month. |
SELECT DATEPART(weekday, GETDATE()); -- e.g., 5 (Friday)
SELECT DATENAME(weekday, GETDATE()); -- e.g., 'Friday'
SELECT YEAR(GETDATE()); -- e.g., 2025
SQL3. DATEADD()
Adds (or subtracts) a time interval to a date.
-- Add 10 days
SELECT DATEADD(day, 10, '2025-05-01'); -- 2025-05-11
-- Subtract 1 month
SELECT DATEADD(month, -1, '2025-05-01'); -- 2025-04-01
SQLThis is critical for rolling time windows, future projections, etc.
4. DATEDIFF()
Calculates the difference between two dates, in terms of a specified date part.
-- Days between dates
SELECT DATEDIFF(day, '2025-01-01', '2025-05-01'); -- 120
-- Months between dates
SELECT DATEDIFF(month, '2024-05-01', '2025-05-01'); -- 12
SQLReturns whole number differences. Always be mindful of truncation behavior.
EOMONTH()
Returns the last day of the month that contains the specified date.
SELECT EOMONTH('2025-05-01'); -- 2025-05-31
SELECT EOMONTH('2025-05-01', 1); -- 2025-06-30
SQLPerfect for financial or monthly reporting.
6. SWITCHOFFSET() and TODATETIMEOFFSET()
Used when working with time zones and datetimeoffset
data types.
-- Adjust to UTC+2
SELECT SWITCHOFFSET(SYSDATETIMEOFFSET(), '+02:00');
-- Set an offset explicitly
SELECT TODATETIMEOFFSET(SYSDATETIME(), '+03:00');
SQLThese functions are crucial for global applications with multiple time zones.
7. CONVERT() and CAST() (For Formatting and Type Conversion)
-- Convert to date only
SELECT CONVERT(date, GETDATE()); -- 2025-05-01
-- Convert to string
SELECT CONVERT(varchar, GETDATE(), 120); -- '2025-05-01 14:35:00'
SQLStyle Code | Format Example |
---|---|
103 | dd/mm/yyyy |
112 | yyyymmdd (ISO) |
120 | yyyy-mm-dd hh:mi:ss (ISO) |
📌 Use CAST()
for ANSI SQL compatibility, and CONVERT()
when style formatting is needed.
Bonus: ISDATE()
Checks if a string can be converted to a valid date.
SELECT ISDATE('2025-05-01'); -- 1 (true)
SELECT ISDATE('not-a-date'); -- 0 (false)
SQLGreat for data validation in dynamic T-SQL scripts.
Summary Table
Function | Purpose |
---|---|
GETDATE() | Current system datetime |
SYSDATETIME() | High-precision current datetime |
DATEPART() | Extract part of a date as int |
DATENAME() | Extract part of a date as string |
DATEADD() | Add/subtract date parts |
DATEDIFF() | Calculate difference between dates |
EOMONTH() | Get last day of the month |
CONVERT() | Format and convert date types |
ISDATE() | Check if value is a valid date |
SWITCHOFFSET() | Adjust timezone for datetimeoffset |
Final Thoughts
Mastering these T-SQL date functions can save you countless hours when dealing with reporting, data transformations, and analytics. Whether you’re building a monthly dashboard, cleaning up dirty data, or optimizing for time-based queries, these tools form the backbone of time manipulation in SQL Server.