To work with dates in a SQL Server query, call a date function inside your SELECT, WHERE or GROUP BY clause — for example SELECT GETDATE() for the current moment, SELECT CAST('2026-12-05' AS date) to turn text into a date, or SELECT DATEADD(day, 7, OrderDate) to shift a date forward a week. Date functions let you capture the current time, build dates from parts, extract a year, month or day, add and subtract intervals, compute the gap between two dates, and round timestamps down to a day, month or year. This guide covers the functions you will use almost every day, in the order you would normally reach for them.
What You'll Need
- Access to a SQL Server instance (any recent version) and a tool that can run queries against it — SQL Server Management Studio, Azure Data Studio, or any client that speaks T-SQL.
- A login with permission to read at least one table, or permission to run ad-hoc queries. If you only want to test functions, no table is required.
- A basic grasp of SELECT, WHERE and ORDER BY, since date functions slot into those clauses.
- Awareness of your session language and DATEFORMAT settings, because literal strings like '12/05/2026' are interpreted differently depending on them.
Menu labels and tool layouts differ slightly between SSMS and Azure Data Studio and between versions, but the T-SQL itself is the same everywhere.
Step-by-Step
- Get the current date and time. Run
SELECT GETDATE()for the server's local date and time, orSELECT SYSDATETIME()for higher precision. UseSELECT SYSUTCDATETIME()when you need UTC regardless of server time zone.CURRENT_TIMESTAMPis an ANSI-standard synonym for GETDATE(). - Get just the date or just the time. Wrap the function in CAST:
SELECT CAST(GETDATE() AS date)returns only the day;SELECT CAST(GETDATE() AS time)returns only the time. Alternatively useCONVERT(date, GETDATE()). Both are equivalent for this purpose. - Build a date from parts. Use
DATEFROMPARTS(2026, 12, 5)to get a date,DATETIME2FROMPARTS(2026, 12, 5, 14, 30, 0, 0, 0)for a precise timestamp, orTIMEFROMPARTS(14, 30, 0, 0, 0)for a time. These are clearer than string concatenation and fail loudly on impossible values such as month 13. - Extract a single part.
SELECT YEAR(OrderDate), MONTH(OrderDate), DAY(OrderDate)returns the components.DATEPART(weekday, OrderDate)gives the day of the week,DATEPART(quarter, OrderDate)the quarter, andDATENAME(month, OrderDate)the month name as text. - Add or subtract intervals.
DATEADD(day, 7, OrderDate)adds a week;DATEADD(month, -1, OrderDate)goes back a month. DATEADD respects calendar rules — adding a month to 31 January lands on the last valid day of February rather than an invalid date. - Measure the gap between two dates.
DATEDIFF(day, StartDate, EndDate)counts the boundaries crossed.DATEDIFF(month, StartDate, EndDate)counts month boundaries, not whole months. For high precision useDATEDIFF_BIGwhen the interval in milliseconds or nanoseconds could overflow a regular integer. - Round a timestamp down. SQL Server has no dedicated TRUNC function for dates. Zero the time with
CAST(OrderDate AS date)for a day,DATEFROMPARTS(YEAR(OrderDate), MONTH(OrderDate), 1)for the first of the month, orDATEFROMPARTS(YEAR(OrderDate), 1, 1)for the first of the year. This is the standard way to group sales by month. - Format a date for display. Use
FORMAT(OrderDate, 'yyyy-MM-dd')orCONVERT(varchar(10), OrderDate, 23)for ISO style, and style 103 for dd/mm/yyyy. FORMAT is the most readable but noticeably slower over large result sets; CONVERT with a style code is faster. - Parse text into a date. Prefer
CAST('2026-12-05' AS date)with unambiguous ISO 8601 strings.TRY_CASTandTRY_CONVERTreturn NULL instead of erroring when the text is bad, which is far safer on imported data. - Filter with dates in WHERE. For a range, use
WHERE OrderDate >= '2026-12-01' AND OrderDate < '2027-01-01'. This half-open pattern captures every timestamp on the final day and lets SQL Server use an index on the column. UseWHERE CAST(OrderDate AS date) = '2026-12-05'only for small tables, because wrapping the column in a function usually blocks an index seek. - Group by a date bucket. Combine step 7 with GROUP BY:
SELECT DATEFROMPARTS(YEAR(OrderDate), MONTH(OrderDate), 1) AS MonthStart, COUNT(*) FROM Orders GROUP BY DATEFROMPARTS(YEAR(OrderDate), MONTH(OrderDate), 1) ORDER BY MonthStart. - Handle time zones deliberately.
SYSDATETIMEOFFSET()returns a value carrying its offset, andAT TIME ZONEconverts between named zones, for exampleSELECT SYSDATETIMEOFFSET() AT TIME ZONE 'Central European Standard Time'. Store timestamps in UTC and convert at display time rather than storing local times.
Common Mistakes
- Ambiguous string literals. '05/12/2026' means 5 December or 12 May depending on session settings. Always use 'yyyy-mm-dd' or 'yyyyMMdd', which SQL Server interprets the same way regardless of language.
- Using BETWEEN on datetimes.
BETWEEN '2026-12-01' AND '2026-12-31'silently drops everything after midnight on the 31st. Use the half-open range instead. - Function on the indexed column.
WHERE YEAR(OrderDate) = 2026prevents a seek. Rewrite as a range comparison. - Assuming DATEDIFF counts whole units. DATEDIFF(year, '2025-12-31', '2026-01-01') returns 1 even though only one day passed, because one year boundary was crossed.
- Expecting GETDATE() to be stable within a query. It is evaluated per statement, and mixing GETDATE() with SYSUTCDATETIME() in the same query can give inconsistent results.
- Trusting FORMAT in big queries. It is convenient but much slower than CONVERT over millions of rows.
Tips
- Use
EOMONTH(OrderDate)to get the last day of a month, orEOMONTH(OrderDate, -1)for the previous month's end — far simpler than manual arithmetic. - Use
DATETRUNCon newer SQL Server versions, which rounds a timestamp down to a given part in one call. On older instances use the DATEFROMPARTS pattern from step 7. - Use
ISDATEfor a quick check, but prefer TRY_CAST for anything you will actually rely on. - Keep date literals in ISO form throughout your scripts so behaviour is identical across servers.
- When comparing dates from different sources, normalise both to UTC first and only then compare.
Troubleshooting
The query returns no rows but data clearly exists. Check whether the column is a datetime rather than a date: filtering for a single day without a time component misses everything after midnight. Widen to a half-open range and retest.
You get a conversion error. A string in an ambiguous format is being read under the wrong language. Rewrite the literal as 'yyyy-mm-dd' and rerun. If the text comes from a file, apply TRY_CONVERT and inspect the rows that return NULL.
Dates look shifted by hours. A UTC value is being shown as local time or the reverse. Confirm the column type, check the server's time zone, and apply AT TIME ZONE explicitly rather than assuming.
A query that used to be fast has slowed down. Look for a function wrapped around a date column in the WHERE clause. Rewrite the predicate as a range so the optimiser can seek.
If that did not work, simplify to the smallest possible query — SELECT GETDATE(), then a literal cast — to confirm the connection and permissions are fine before adding your real logic back one clause at a time.
Frequently Asked Questions
How do I get just today's date without the time?
Run SELECT CAST(GETDATE() AS date). That returns a date value with no time component.
What is the difference between GETDATE and SYSDATETIME?
Both return the current local time; SYSDATETIME returns a datetime2 with higher fractional-second precision.
How do I find the last day of the month?
Use EOMONTH(yourdate). It handles leap years and varying month lengths automatically.
Why does my date filter miss the last day's rows?
Because the column holds a time as well as a date, and your upper bound is midnight. Use >= first day AND < next day after the range.
Can I store dates and times in different time zones?
Yes. datetimeoffset records an offset alongside the value, and AT TIME ZONE converts between named zones.
Should I use FORMAT or CONVERT for display?
FORMAT is easier to read and supports .NET-style patterns; CONVERT with style codes performs better in large queries.