There are three ways to remove the time part of a datetime in SQLServer:
Use the DATE data type
The DATE data type only stores the date part of a datetime value. To use the
DATE data type, you need to cast the datetime value to DATE. For example, the following code will remove the time part of the datetime value
2023-07-12 08:00:25:
SQL
SELECT CAST('2023-07-12 08:00:25' AS DATE);
Use the CONVERT function
The CONVERT function can be used to convert a datetime value to a different data type. To remove the time part of a datetime value, you can use the
CONVERT function to convert the datetime value to the DATE data type. For example, the following code will remove the time part of the datetime value
2023-07-12 08:00:25:
SQL
SELECT CONVERT(DATE, '2023-07-12 08:00:25');
Use the SUBSTRING function
The SUBSTRING function can be used to extract a substring from a string. To remove the time part of a datetime value, you can use the
SUBSTRING function to extract the first 10 characters from the datetime value. The first 10 characters of a datetime value represent the date part of the datetime value. For example, the following code will remove the time part of the datetime value 2023-07-12 08:00:25:
SQL
SELECT SUBSTRING('2023-07-12 08:00:25', 1, 10);
The best way to remove the time part of a datetime in SQL Server depends on your specific needs. If you need to store the date part of the datetime value in a separate column, you can use the
DATE data type. If you need to store the date part of the datetime value in the same column, you can use the
CONVERT function or the SUBSTRING function.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
There are three ways to remove the time part of a datetime in SQL Server:
DATEdata typeThe
DATEdata type only stores the date part of a datetime value. To use theDATEdata type, you need to cast the datetime value toDATE. For example, the following code will remove the time part of the datetime value2023-07-12 08:00:25:SQL
CONVERTfunctionThe
CONVERTfunction can be used to convert a datetime value to a different data type. To remove the time part of a datetime value, you can use theCONVERTfunction to convert the datetime value to theDATEdata type. For example, the following code will remove the time part of the datetime value2023-07-12 08:00:25:SQL
SUBSTRINGfunctionThe
SUBSTRINGfunction can be used to extract a substring from a string. To remove the time part of a datetime value, you can use theSUBSTRINGfunction to extract the first 10 characters from the datetime value. The first 10 characters of a datetime value represent the date part of the datetime value. For example, the following code will remove the time part of the datetime value2023-07-12 08:00:25:SQL
The best way to remove the time part of a datetime in SQL Server depends on your specific needs. If you need to store the date part of the datetime value in a separate column, you can use the
DATEdata type. If you need to store the date part of the datetime value in the same column, you can use theCONVERTfunction or theSUBSTRINGfunction.