Working with date and time values in SQL Server often requires extracting only the calendar date portion while discarding the time component. Think about it: whether you're generating reports, filtering records for a specific day, or preparing data for migration, knowing how to reliably get date from datetime sql server is a fundamental T-SQL skill. The DATETIME data type has been a cornerstone of SQL Server since its early versions, storing both date and time to a precision of 3.Day to day, 33 milliseconds. Still, applications frequently need only the date part for grouping, sorting, or comparing against calendar days. This article walks through the most effective methods, their nuances, and best practices so you can choose the right approach for any scenario And it works..
Understanding SQL Server DateTime Fundamentals
Before diving into extraction techniques, it helps to understand how SQL Server represents temporal data. The DATETIME type ranges from January 1, 1753 to December 31, 9999, and always includes both date and time. When you store a value like '2023-10-05 14:35:22', the database engine retains the full timestamp. Functions that "get date from datetime sql server" essentially mask or convert the time portion away, leaving only the year, month, and day. Another common type, DATETIME2, offers greater precision and a wider range, but the extraction methods remain largely similar. Understanding this foundation prevents common errors such as unexpected time shifts or format mismatches when the date is later used in string comparisons or joins.
Using CAST and CONVERT for Date Extraction
The most straightforward ways to get date from datetime sql server involve the CAST and CONVERT functions. Both are T-SQL staples, but they behave slightly differently regarding output style. CAST(my_datetime AS DATE) is the ANSI-standard approach available since SQL Server 2008 and returns a DATE type value representing midnight of the specified day. For example:
SELECT CAST(GETDATE() AS DATE) AS TodayOnly;
This returns 2023-10-05 (assuming today's date) with the time component zeroed out.
CONVERT, on the other hand, gives you more control over the display format. Also, if you need the date in a specific format for display purposes while still working with a DATE type, you might write:
SELECT CONVERT(VARCHAR(10), my_datetime, 101) AS FormattedDate;
Style 101 yields mm/dd/yyyy. Using CONVERT(DATE, my_datetime) achieves the same result as CAST, but CONVERT also accepts a style parameter that influences how the date appears when converted to a string. Still, for pure date extraction without formatting, `CAST(.. And that's really what it comes down to..
This is where a lot of people lose the thread Simple, but easy to overlook..
Truncating Time with DATEADD and DATEDIFF
When you need to keep compatibility with older SQL Server versions (pre‑2008) or you simply prefer a pure datetime result rather than a date, the classic pattern is to use DATEADD together with DATEDIFF. The expression DATEADD(day, DATEDIFF(day, 0, @dt), 0) essentially “rounds down” the supplied datetime to the start of its calendar day.
DECLARE @dt DATETIME = '2023-10-05 14:35:22';
SELECT DATEADD(day, DATEDIFF(day, 0, @dt), 0) AS TruncatedDate;
-- Returns: 2023-10-05 00:00:00
Why it works: DATEDIFF(day, 0, @dt) calculates how many whole days have elapsed since the “zero” date (1900‑01‑01). Multiplying that by DATEADD and adding 0 resets the time portion to midnight. This method is strong, works on every version of SQL Server, and does not require the DATE data type.
Performance note: The double‑function call is a bit more expensive than a simple CAST(... AS DATE), especially when applied to large tables. On the flip side, the difference is usually negligible unless you are aggregating millions of rows per second. If you anticipate heavy usage, a persisted computed column that stores the truncated date can off‑load the calculation to the engine’s index maintenance Worth keeping that in mind..
Using CONVERT for a String‑Based Date
If you only need a readable date string for reports or UI display, CONVERT with an appropriate style code can be a quick one‑liner. Style 23 (yyyy‑mm‑dd) and style 112 (yyyymmdd) are popular because they produce ISO‑like formats without extra punctuation.
SELECT CONVERT(VARCHAR(10), @dt, 23) AS IsoDate; -- 2023-10-05
SELECT CONVERT(VARCHAR(10), @dt, 112) AS CompactDate; -- 20231005
When the string
When the string representation is required, the most straightforward approach is to apply CONVERT (or its synonym CAST) with a style that matches the desired layout. For an ISO‑8601‑compatible format, style 23 returns yyyy‑mm‑dd, while style 112 yields a compact yyyymmdd that is ideal for sorting or joining. If you need a locale‑specific pattern — say
If you need a locale‑specific pattern — say you want a formal “Monday, October 5, 2023” for a report, or a regional “05/10/2023” for a European audience — SQL Server gives you a couple of reliable ways to achieve it without sacrificing the underlying datetime value Still holds up..
Using FORMAT for Human‑Readable Strings
Introduced in SQL Server 2012, FORMAT lets you apply .NET‑style formatting directly to date and time values. The function respects the session’s SET LANGUAGE and SET DATEFORMAT settings, so you can generate strings that match the conventions of your users.
DECLARE @dt DATETIME = '2023-10-05 14:35:22';
/* Full, locale‑aware name */
SELECT FORMAT(@dt, N'dddd, MMMM dd, yyyy') AS FormalDate; -- Monday, October 05, 2023
/* European short style (dd/MM/yyyy) */
SELECT FORMAT(@dt, N'dd/MM/yyyy') AS EuDate; -- 05/10/2023
/* Custom pattern with ordinal suffix (requires a little extra logic) */
SELECT
FORMAT(@dt, N'yyyy-MM-dd') AS IsoDate,
CAST(DATEPART(day,@dt) AS VARCHAR) +
CASE WHEN DATEPART(day,@dt) % 10 IN (1,2,3) AND DATEPART(day,@dt) / 10 <> 1
THEN CASE DATEPART(day,@dt) % 10 WHEN 1 THEN 'st' WHEN 2 THEN 'nd' WHEN 3 THEN 'rd' ELSE 'th' END
ELSE 'th' END AS OrdinalDay; -- 2023‑10‑05, 5th
Tip: If you need the formatted string to be stored as data (e.g., in a
NVARCHARcolumn), consider adding a persisted computed column. This off‑loads the formatting cost to the index build and makes the result set‑friendly for joins Not complicated — just consistent. Surprisingly effective..
Leveraging CONVERT with Locale‑Specific Styles
While FORMAT is the most expressive, CONVERT with a numeric style can also be tuned to regional needs by adjusting the session’s date‑format settings. As an example, after SET DATEFORMAT dmy, style 5 (dd/mm/yy) will produce the European layout:
SET DATEFORMAT dmy;
SELECT CONVERT(VARCHAR(10), @dt, 5) AS DayMonthYear; -- 05/10/2023
Be aware that CONVERT styles are limited to a predefined set; they cannot generate the full flexibility of FORMAT. Still, for complex patterns (e. g., “5th October 2023”) you’ll still lean on FORMAT or custom string manipulation Worth keeping that in mind..
Extracting Pure Date Parts
Sometimes you only need the year, month, or day as integers for grouping or filtering. The simplest, most performant way is to cast the datetime to DATE and then use YEAR(@dt), MONTH(@dt), etc.:
SELECT
CAST(@dt AS DATE) AS TruncDate,
YEAR(CAST(@dt AS DATE)) AS Year,
MONTH(CAST(@dt AS DATE)) AS Month,
DAY(CAST(@dt AS DATE)) AS Day;
If you must keep the original datetime type (for compatibility with older schemas), the DATEADD/DATEDIFF trick remains a solid fallback:
SELECT
DATEADD(day, DATEDIFF(day, 0, @dt), 0) AS TruncDate,
DATEPART(year, DATEADD(day, DATEDIFF(day, 0, @dt), 0)) AS Year,
DATEPART(month, DATEADD(day, DATEDIFF(day, 0, @dt), 0)) AS Month,
DATEPART(day, DATEADD(day, DATEDIFF(day, 0, @dt), 0)) AS Day;
Performance Considerations
| Technique | Typical I/O | CPU
| Technique | Typical I/O | CPU Cost | Flexibility | Use Case |
|---|---|---|---|---|
FORMAT |
Low | High | Maximum | Complex, locale-aware display formatting |
CONVERT (style-based) |
Low | Very Low | Limited | Standard formats, performance-critical paths |
DATEPART / YEAR / MONTH / DAY |
Low | Very Low | None (returns integers) | Grouping, filtering, date arithmetic |
CAST to DATE |
Low | Very Low | None (truncates time) | Stripping time component efficiently |
DATEADD(DATEDIFF(...)) |
Low | Low | None | Legacy compatibility, time truncation |
Best Practices Summary
- Prefer
DATEPARTfor numeric extraction – it’s the fastest way to get individual date components for grouping or calculations. - Use
CONVERTfor standard formats – when you need a quick string representation and performance matters more than flexibility. - Reserve
FORMATfor presentation layer – its .NET underpinnings make it slower, so avoid using it in large result sets or tight loops. - Avoid repeated conversions in loops – pre-compute and store formatted values if they’ll be reused frequently.
- Consider indexed computed columns – for frequently accessed formatted dates, a persisted computed column ensures optimal read performance.
Conclusion
Mastering datetime formatting in SQL Server requires balancing performance and functionality. While FORMAT offers unmatched flexibility for creating human-readable date strings, simpler functions like CONVERT, DATEPART, and CAST provide efficient alternatives for standard operations. By understanding the strengths and limitations of each approach—and applying them appropriately based on context—you can write queries that are both performant and maintainable. Whether you're generating reports, building APIs, or optimizing data pipelines, choosing the right datetime function ensures your SQL code scales effectively across diverse application requirements.