SQL Query to Convert DateTime to Date in SQL Server
Last Updated :
20 Nov, 2024
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.
Similar Reads
SQL Query to Convert Date to Datetime
In this article, we will look at how to convert Date to Datetime. We can convert the Date into Datetime in two ways. Using CONVERT() function: Convert means to change the form or value of something. The CONVERT() function in the SQL server is used to convert a value of one type to another type.Conve
1 min read
SQL Query to Convert Datetime to Date
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 commo
4 min read
SQL Query to Convert Datetime to String
In order to convert a DateTime to a string, we can use CONVERT() and CAST() function. These functions are used to converts a value(of any datatype) into a specified datatype. CONVERT() Function Syntax: CONVERT(VARCHAR, datetime [,style])VARCHAR - It represent the string type.datetime - It can be the
3 min read
SQL Query to Convert Date Range to Per Day Records
In this article, we are going to see how we can convert the date range to per day records, but before that, we need to have a clear idea of where it is going to be helpful for us. Suppose, We have a database where we want to store all the project details records of the students of a particular class
5 min read
SQL Query to Convert Datetime to Epoch
Converting a datetime value to Epoch time is a common operation in SQL, particularly when working with timestamps in various applications. In this article, We will learn a step-by-step process of creating a SQL database, inserting datetime values and converting those values into Epoch time using SQL
4 min read
How to Convert DateTime to VarChar in SQL Server
In SQL Server, date and time values are often stored in the DATETIME or DATE data types. However, there are situations where we may want to convert these values into a different format such as VARCHAR to display the date in a specific format or for further manipulation. In this article, we will expl
4 min read
Convert Date To Datetime In Python
When you're programming, dealing with dates and times is important, and Python provides tools to manage them well. This article is about changing dates into date times in Python. We'll explore methods that can help you switch between these two types of data. Whether you're building a website or work
3 min read
SQL Query to Convert Date Field to UTC
In SQL, dates are complicated for newbies, since while working with the database, the format of the date in the table must be matched with the input date in order to insert. In various scenarios instead of date, DateTime (time is also involved with date) is used. In this article, we will discuss how
2 min read
How to convert datetime to date in Python
In this article, we are going to see how to convert DateTime to date in Python. For this, we will use the strptime() method and Pandas module. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date() function and dt.date f
3 min read
How to Convert Datetime to Date in Pandas ?
DateTime is a collection of dates and times in the format of "yyyy-mm-dd HH:MM:SS" where yyyy-mm-dd is referred to as the date and HH:MM:SS is referred to as Time. In this article, we are going to discuss converting DateTime to date in pandas. For that, we will extract the only date from DateTime us
4 min read