How to convert datetime to date (with time set to 00:00:00.000) in SQL
By FoxLearn 10/7/2024 9:21:02 AM 146
To convert a DATETIME value to a DATE only (with the time set to 00:00:00.000) in SQL, you can use various functions depending on the SQL database you are using.
If you want to convert a string like '2024-10-07 08:50:12.000'
to a DATETIME
in SQL Server while ignoring the time and setting it to 00:00:00.000
, you can use the CAST
or CONVERT
functions to extract just the date part and set the time to midnight.
SELECT CAST(CONVERT(VARCHAR(10),'2024-10-07 08:50:12.000', 101) AS DATETIME) //2024-10-07 00:00:00.000
Syntax
SELECT CAST(your_datetime_column AS DATE) AS date_only FROM your_table
For example:
SELECT CAST(CreatedDate AS DATE) AS CreatedDate FROM Products SELECT CAST(CONVERT(VARCHAR, GETDATE(), 102) AS DATETIME) //2024-10-07 00:00:00.000 SELECT CONVERT(CHAR(10), CAST('2024-10-07 08:50:12.000' AS DATETIME), 101) //10/07/2024 SELECT CONVERT(VARCHAR, CONVERT (DATETIME, '2024-10-07 08:50:12.000'), 101) //10/07/2024
- How to fix 'The transaction log for the database is null due to OLDEST_PAGE'
- How to convert varchar to uniqueidentifier in SQL Server
- How to convert timestamp to date in SQL Server
- How to Download and Restore Northwind database to SQL Server
- How to Download Microsoft SQL Server
- Saving changes is not permitted in SQL Server
- How to change ‘Edit Top 200 Rows’ and ‘Select Top 1000 Rows’ in SQL
- How to fix 'The specified sa password does not meet strong password requirements'
Categories
Popular Posts
AdminKit Bootstrap 5 HTML5 UI Kits Template
11/17/2024
Spica Admin Dashboard Template
11/18/2024