What is the best way to remove the time part of datetime in SQL Server?
What is the best way to remove the time part of datetime in SQL Server?
465
11-Jul-2023
Updated on 12-Jul-2023
Aryan Kumar
12-Jul-2023There 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.