在一个日期中省略毫秒 [英] Omitting the Milliseconds in a Date

查看:112
本文介绍了在一个日期中省略毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从SQL Server中选择时,我想获取一个日期,但省略毫秒值,我希望它作为日期类型。所以如果我有一个值 1/1/2009 1:23:11.923 ,我想省略毫秒,但保留日期类型,因此它将是值 1/1/2009 1:23:11.000

When I select from SQL Server, I want to get a date, but omit the millisecond value, and I want it to be as a date type. So if I have a value 1/1/2009 1:23:11.923, I want to omit the millisecond but retain the date type, so that it will be the value 1/1/2009 1:23:11.000 (I know you really can't omit the millisecond value with a date, just want it to be zero).

在SQL Server中有这样的功能吗?还是我必须写我自己的功能?再次,我不希望它作为 varchar 类型,而是 datetime 类型。

Is there a function in SQL Server to do this? Or do I have to write my own function? Again, I don't want it as a varchar type, but a datetime type.

推荐答案

如果你不想使用字符串转换,这里有一个解决方案:

If you don't want to use string conversions, here's a solution:

DECLARE @TheDate datetime, @Today datetime
SET @TheDate = GetDate()

SET @Today = DateAdd(dd, DateDiff(dd, 0, @TheDate), 0)
SELECT DateAdd(s, DateDiff(s, @Today, @TheDate), @Today)

这篇关于在一个日期中省略毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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