Welcome back. Date/time bugs are some of the most expensive problems in real systems.
A report can be “wrong” for months and nobody notices, or a job can fail silently because a filter
excluded the correct rows. DBAs are expected to prevent these issues.
In this lesson you will learn how SQL Server represents time, the differences between the core date/time functions,
how to filter dates safely (without breaking indexes), and how to think about time zones in production systems.
Lesson description: You will learn the most common SQL Server date/time data types,
the exact differences between GETDATE(), SYSDATETIME(), GETUTCDATE(), and SYSUTCDATETIME(),
why precision matters, how implicit conversions cause performance issues, how to write SARGable date filters,
how to handle “whole day” filtering correctly, and how to reason about time zones (UTC vs local time)
so your reports, jobs, and audits stay accurate across regions and daylight saving time.
Date/time problems happen because applications and databases often combine:
datetime vs datetime2 vs date)DBA mindset: Always treat date/time as “requires proof,” not “probably fine.”
datetime2(0) (seconds) through datetime2(7) (100ns).DBA best practice: For new designs, prefer datetime2 (or datetimeoffset if offsets are required).
These are core functions DBAs see everywhere. They are not the same.
GETDATE() returns current server local time as datetime.SYSDATETIME() returns current server local time as datetime2(7) (higher precision).GETUTCDATE() returns current UTC time as datetime.SYSUTCDATETIME() returns current UTC time as datetime2(7).SELECT
GETDATE() AS GetDate_Local_datetime,
SYSDATETIME() AS SysDateTime_Local_datetime2,
GETUTCDATE() AS GetUtcDate_UTC_datetime,
SYSUTCDATETIME() AS SysUtcDateTime_UTC_datetime2;
DBA rule: Use SYSDATETIME() / SYSUTCDATETIME() when you want modern precision
and to align with datetime2 columns.
DBA reality: In many companies, the standard is:
store timestamps in UTC (datetime2) and convert to local time only at the presentation layer.
One common incident: “the app says the event happened at 10:15:12.987, but SQL shows 10:15:12.986 or 10:15:12.990.”
This usually happens because:
datetime rounds to increments (not all milliseconds exist)datetime2 for stored timestamps.datetime2(3) milliseconds or datetime2(0) seconds).DBAs care about something called SARGability:
can SQL Server use an index seek, or will it scan because you wrapped a column in a function?
This forces SQL Server to apply a function to every row:
-- BAD: Function on the column SELECT * FROM Sales.SalesOrderHeader WHERE CAST(OrderDate AS date) = '2014-06-01';
Use a range that covers the entire day:
-- GOOD: Range filter (index-friendly) DECLARE @StartDate date = '2014-06-01'; SELECT * FROM Sales.SalesOrderHeader WHERE OrderDate >= @StartDate AND OrderDate < DATEADD(DAY, 1, @StartDate);
DBA rule: Never wrap an indexed date column in CAST/CONVERT in the WHERE clause for filtering.
Use a range instead.
Many beginners write:
-- Risky for datetime/datetime2: WHERE OrderDate BETWEEN '2014-06-01' AND '2014-06-30'
This is ambiguous because ‘2014-06-30’ is interpreted as midnight at the start of the day,
so you can accidentally exclude everything after 00:00:00 on the end date.
DECLARE @StartDate date = '2014-06-01'; DECLARE @EndDate date = '2014-06-30'; SELECT * FROM Sales.SalesOrderHeader WHERE OrderDate >= @StartDate AND OrderDate < DATEADD(DAY, 1, @EndDate);
DBA habit: Use “greater than or equal to start” and “less than next day.”
This avoids end-of-day bugs for all precisions.
SQL Server itself does not automatically “know” what time zone a datetime represents.
A value like 2026-01-10 09:00:00 is meaningless unless you know:
datetime2.Use datetimeoffset if the time zone offset must be preserved with the event.
For example: travel bookings, scheduling across countries, compliance logs requiring original offset.
SQL Server supports time zone conversion using AT TIME ZONE (available in modern versions).
The typical safe pattern is:
Example pattern (use a real time zone name that matches your system requirements):
-- Example: Convert a UTC timestamp to Eastern Time (concept)
DECLARE @UtcTime datetime2(0) = SYSUTCDATETIME();
SELECT
@UtcTime AS UtcTime,
(@UtcTime AT TIME ZONE 'UTC') AT TIME ZONE 'Eastern Standard Time' AS EasternTime;
DBA caution: Time zone conversion can be expensive in large queries.
For reporting at scale, many systems pre-compute local time in the app layer or in reporting models.
Do these in AdventureWorks and save the scripts:
Next, you will learn how DBAs write scripts that fail safely: TRY/CATCH, transactions, rollback logic,
logging outputs, and “no surprises” behavior in production operations.
Not a member yet? Register now
Are you a member? Login now