Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert datetime to date only (with time set to 00:00:00.000)

I have a string '2009-06-24 09:52:43.000', which I need to insert to a DateTime column of a table.

But I don't care about the time, just want to insert it as 2009-06-24 00:00:00.000

How can I do that in T-SQL?

like image 904
Saobi Avatar asked Jun 24 '09 20:06

Saobi


People also ask

How do I convert DateTime to date format?

You can convert a DATETIME to a DATE using the CONVERT function. The syntax for this is CONVERT (datetime, format). This shows the date only and no time.

How do I convert DateTime to just time?

DateTime testDateTime = new DateTime(2021,09,21,03,13,22);// Creating TimeOnly object from DateTime.

How do I get only the date in SQL without time?

However, if you want to get just the current date – not the date and the time – you can use the CAST() function. This function takes any expression or any column name as the first argument. Then you use the keyword AS and enter the new data type to return.


1 Answers

For SQL Server 2005 and below:

CONVERT(varchar(8), @ParamDate, 112)    -- Supported way

CAST(FLOOR(CAST(@ParamDate AS float)) AS DATETIME)   -- Unsupported way

For SQL Server 2008 and above:

CAST(@ParamDate AS DATE)
like image 136
GSerg Avatar answered Oct 02 '22 14:10

GSerg