How to convert timestamp to date in SQL Server
By Tan Lee Published on Dec 09, 2024 793
In SQL Server, you can convert a timestamp (or datetime) to just a date using the CAST or CONVERT functions.
How to convert timestamp to date in sql server?
The CAST
function is straightforward and is the most common way to convert a timestamp (datetime) to a date:
For example, sql query to convert timestamp to date
SELECT GETDATE() // 2024-12-09 21:28:06.843 SELECT CAST(GETDATE() AS DATE) // 2024-12-09
The CONVERT
function also works well to convert a timestamp to a date:
SELECT CONVERT(DATE, GETDATE()) // 2024-12-09
The GETDATE() function returns the current date and time in SQL Server.
The CAST(... AS DATE) function converts the timestamp value to only the date (removing the time).
The CONVERT(DATE, ...) is similar to CAST, but you can customize its style if needed.
How to convert timestamp to datetime in SQL?
For example, you need to convert "2024-04-29T07:20:32.727Z" to "2024-04-29 07:20:32".
You can use cast()
// sql server convert timestamp to date select cast('2024-04-29T07:20:32.727Z' as datetime)
Output:
2024-04-29 07:20:32.727
- How to Download ODBC Driver for SQL Server
- How to Download SQL Server Management Studio (SSMS) Versions
- How to Query JSON in SQL Server
- How to modify JSON in SQL Server
- How to set time to 00:00:00 with GETDATE() in SQL
- How to find all the dependencies of a table in SQL Server
- How to Find Objects Referencing a Table in SQL Server
- Case sensitivity in SQL Server
Categories
Popular Posts
Portal HTML Bootstrap
Nov 13, 2024
Freedash bootstrap lite
Nov 13, 2024
Implementing Caching in ASP.NET Core
Dec 14, 2024
11 Things You Didn't Know About Cloudflare
Dec 19, 2024