Open In App

SQL Query to Convert DateTime to Date in SQL Server

Last Updated : 20 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In SQL Server, working with DateTime data types can be a bit complex for beginners. This is because DateTime includes both the date and time components, while many scenarios only require the date. Whether you’re working with large datasets, performing data analysis, or generating reports, it’s common to need the date portion only.

In this article, we will explore how to convert DateTime to Date in SQL Server using four powerful methods: CAST(), CONVERT(), TRY_CONVERT(), and SUBSTRING(). By the end of this guide, you will understand how to extract the date part of a DateTime value and streamline your queries.

Why Convert DateTime to Date in SQL Server?

Converting DateTime to Date is essential in various scenarios where the time portion is irrelevant. For example:

  • Generating date-based reports where time does not matter.
  • Simplifying date comparisons for data analysis.
  • Performing aggregate calculations that only focus on the date (e.g., grouping by day).

Methods to Convert DateTime to Date in SQL Server

The aim of this article data is to convert DateTime to Date in SQL Server like YYYY-MM-DD HH:MM: SS to YYYY-MM-DD. Here are four common methods to convert DateTime to Date in SQL Server:

1. Using CAST()

The CAST() function is a flexible SQL Server function used to explicitly convert one data type to another. To convert a DateTime to a Date, we can use the CAST() function as shown below.

Syntax

CAST( dateToConvert AS DATE)

Example 1

Convert the current DateTime to a Date.

Query:

SELECT CAST(GETDATE() AS DATE) AS CURRENT_DATE

Output

Explanation:

In this example, GETDATE() returns the current DateTime, and the CAST() function converts it to a Date.

Example 2

Convert a specific DateTime string to a Date.

Query:

SELECT CAST('2021-08-27 17:26:36.710' AS DATE) AS CURRENT_DATE_GFG

Output

2. Using CONVERT()

The CONVERT() function is another method to convert DateTime to Date in SQL Server. It allows us to specify the target data type as DATE and is particularly useful when working with different date formats.

Syntax

CONVERT(DATE, dateToConvert)

Example 1

Convert the current DateTime to a Date.

Query:

SELECT CONVERT(DATE, GETDATE()) AS CURRENT_DATE_GFG

Output

Example 2

Convert a specific DateTime string to a Date.

Query:

SELECT CONVERT(DATE, '2021-08-27 17:26:36.710' ) AS CURRENT_DATE_GFG

Output

3. Using TRY_CONVERT()

The TRY_CONVERT() function is similar to CONVERT(), but with an important distinction: if the conversion fails, it returns NULL instead of throwing an error. This can be particularly useful when we are working with data integrity and error handling.

Syntax

TRY_CONVERT(DATE, dateToConvert)

Example 1:

Try converting the current DateTime to Date.

Query

SELECT TRY_CONVERT(DATE,GETDATE()) AS CURRENT_DATE_GFG

Output

Example 2:

Convert a valid DateTime string to Date.

Query:

SELECT TRY_CONVERT(DATE,'2021-08-27 17:26:36.710') AS CURRENT_DATE_GFG

Output

4. Using SUBSTRING()

You can also use the SUBSTRING() function to extract the date portion of a DateTime. This method involves getting a substring of the DateTime string, starting from the 0th index and selecting only the first 10 characters (YYYY-MM-DD).

Syntax

SUBSTRING( dateToConvert ,0,11)

Example 1

Extract the date from a DateTime string.

Query:

SELECT SUBSTRING( '2021-08-27 17:26:36.710' ,0,11) AS CURRENT_DATE_GFG

Output

Example 2

Extract the date from the current DateTime using CONVERT() to a varchar format.

Query:

SELECT SUBSTRING( CONVERT(varchar(17), GETDATE(), 23) ,0,11) AS CURRENT_DATE_GFG

Output

Conclusion

Converting DateTime to Date in SQL Server is a straightforward process that can help simplify our queries by focusing only on the date portion of the value. Whether we’re using CAST(), CONVERT(), TRY_CONVERT(), or SUBSTRING(), each method allows us to effectively manage DateTime values in our SQL queries. By choosing the appropriate method for our use case, we can enhance query performance, improve data analysis, and generate accurate reports without the need for time-related data.



Next Article

Similar Reads