如何仅将日期时间转换为日期(时间设置为 00:00:00.000)
问题描述:
我有一个字符串 '2009-06-24 09:52:43.000',我需要将其插入到表的 DateTime 列中.
I have a string '2009-06-24 09:52:43.000', which I need to insert to a DateTime column of a table.
但我不在乎时间,只想插入为 2009-06-24 00:00:00.000
But I don't care about the time, just want to insert it as 2009-06-24 00:00:00.000
我如何在 T-SQL 中做到这一点?
How can I do that in T-SQL?
答
对于 SQL Server 2005 及以下:
For SQL Server 2005 and below:
CONVERT(varchar(8), @ParamDate, 112) -- Supported way
CAST(FLOOR(CAST(@ParamDate AS float)) AS DATETIME) -- Unsupported way
对于 SQL Server 2008 及更高版本:
For SQL Server 2008 and above:
CAST(@ParamDate AS DATE)