如何仅将日期时间转换为日期(时间设置为 00:00:00.000) [英] How to convert datetime to date only (with time set to 00:00:00.000)

查看:56
本文介绍了如何仅将日期时间转换为日期(时间设置为 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)

这篇关于如何仅将日期时间转换为日期(时间设置为 00:00:00.000)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆