Convert Datetime To Date In Sql

4 min read

Convert datetime to date in SQL is a common task when you need to strip the time portion from a timestamp and work solely with the calendar date. Whether you are building reports, filtering records, or normalizing data for comparison, knowing how to reliably convert a datetime value to a date type simplifies queries and improves performance. This guide walks you through the concept, demonstrates multiple conversion techniques across popular database systems, highlights performance tips, and answers frequently asked questions so you can apply the solution confidently in any SQL environment.


Introduction

In relational databases, a datetime column stores both date and time information (e.Which means g. , 2025-11-02 14:35:07.Consider this: 123). Many business rules, however, only care about the date part—think of daily sales totals, birthday matching, or grouping events by day. Converting datetime to date removes the time component, yielding a value like 2025-11-02. That's why the operation is straightforward, but the exact syntax varies between SQL Server, MySQL, PostgreSQL, and Oracle. Understanding the underlying mechanics helps you choose the most efficient method and avoid subtle bugs such as implicit conversions or timezone mishaps.


Understanding DATETIME and DATE Data Types

Before diving into conversion techniques, it helps to clarify what each type represents:

  • DATETIME (or TIMESTAMP): Stores date and time with a precision that can range from seconds to fractions of a second, depending on the platform. Internally, it is often saved as a numeric offset from a base date (e.g., SQL Server counts days since 1900‑01‑01).
  • DATE: Holds only the calendar date (year, month, day) with no time component. Storage is typically more compact because the time portion is omitted.

When you cast or convert a datetime to a date, the database discards the time part without rounding. As an example, 2025-11-02 23:59:59 becomes 2025-11-02, not 2025-11-03.


Methods to Convert datetime to date in SQL

Different SQL dialects provide built‑in functions to achieve the conversion. Below are the most portable and performant approaches.

1. Using CAST (ANSI‑SQL Standard)

The CAST function is part of the SQL standard and works in virtually every major RDBMS The details matter here. Still holds up..

SELECT CAST(your_datetime_column AS DATE) AS date_only
FROM   your_table;
  • Why it works: CAST tells the engine to reinterpret the binary representation of the datetime as a date, truncating the time.
  • Performance: Usually optimized to a simple column rewrite; no extra computation is needed.

2. Using CONVERT (SQL Server Specific)

SQL Server offers CONVERT with style codes, which can be handy when you also need a specific string format.

SELECT CONVERT(DATE, your_datetime_column) AS date_only
FROM   your_table;
  • Style codes: If you later need a string, you can add a style (e.g., CONVERT(VARCHAR(10), your_datetime_column, 120) yields yyyy-mm-dd). For pure date extraction, the style argument is optional.

3. Using FORMAT (SQL Server 2012+)

FORMAT returns a string, so you would typically cast it back to date if you need the native type.

SELECT TRY_CAST(FORMAT(your_datetime_column, 'yyyy-MM-dd') AS DATE) AS date_only
FROM   your_table;
  • Note: FORMAT is less efficient than CAST/CONVERT because it involves formatting logic; use it only when you need localized strings.

4. MySQL: DATE() Function

MySQL provides a dedicated DATE() function that extracts the date part directly.

SELECT DATE(your_datetime_column) AS date_only
FROM   your_table;
  • Equivalent CAST: CAST(your_datetime_column AS DATE) works as well and is ANSI‑compliant.

5. PostgreSQL: ::DATE Cast or DATE() Function

PostgreSQL supports both the ANSI cast and a PostgreSQL‑specific shorthand Not complicated — just consistent..

SELECT your_datetime_column::DATE AS date_only
FROM   your_table;
-- or
SELECT DATE(your_datetime_column) AS date_only
FROM   your_table;

6. Oracle: TRUNC Function

In Oracle, TRUNC strips the time portion when the format model is omitted or set to 'DD'.

SELECT TRUNC(your_datetime_column) AS date_only
FROM   your_table;
  • Alternative: CAST(your_datetime_column AS DATE) also works, but note that Oracle’s DATE datatype still includes a time component (midnight). If you truly need a pure date without time, consider using the TIMESTAMP type and truncating, or store the result in a VARCHAR for display.

7. Using DATEADD/DATEDIFF Trick (SQL Server)

A classic method that avoids reliance on newer functions:

SELECT DATEADD(day, DATEDIFF(day, 0, your_datetime_column), 0) AS date_only
FROM   your_table;
  • How it works: DATEDIFF counts the number of days between the base date (0, i.e., 1900‑01‑01) and the datetime; DATEADD then adds that many days back to the base, landing at midnight of the target day.
  • Use case: Helpful when you need compatibility with very old SQL Server versions.

Database‑Specific Examples

Below are ready‑to‑run snippets for each major platform. Replace your_table and your_datetime_column with your actual identifiers.

SQL Server

-- Using CAST
SELECT CAST(order_date AS DATE) AS order_only
FROM   Orders;

-- Using CONVERT
SELECT CONVERT(DATE, order_date) AS order_only
FROM   Orders;

-- Using DATEADD/D
New on the Blog

What People Are Reading

Readers Went Here

What Goes Well With This

Thank you for reading about Convert Datetime To Date In Sql. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home