在mssql 2005中保存DateTime,不需要几分钟和几秒钟 [英] Save DateTime in mssql 2005 without hours, minutes and seconds

查看:161
本文介绍了在mssql 2005中保存DateTime,不需要几分钟和几秒钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将日期保存在sql2005中作为日期(不分小时和秒)。
我想这样做,因为如果填写小时,分钟和秒数,函数之间的函数并不总是正确的。



但是datetime和smalldatetime都不允许这个,2008年你有Date列可以做到这一点。



我也发现这个问题有助于我,但我不喜欢演员执行我的查询:如何删除日期时间值(SQL Server)的时间部分?

解决方案

非转换方式:

  SET @d = DATEADD(dd,0,DATEDIFF(dd,0,@dt))

将0和您的日期之间的天数添加到0,导致时间部分为零。 p>

如果要比显式CAST方法快,必须进行测试。使用0表示将在此表达式中执行一些隐式转换。


I want to save my dates in sql2005 as dates (without hour minutes and seconds). I want to do this because the between function isn't always correct if the hours, minutes and seconds are filled in.

but neither datetime nor smalldatetime allows this, in 2008 you have the Date column which can do this.

I've also found this question which helps me out but I don't like the casting to execute my queries: How to remove the time portion of a datetime value (SQL Server)?

解决方案

The non-casting way:

SET @d = DATEADD(dd, 0, DATEDIFF(dd, 0, @dt))

This adds the number of days between 0 and your date to 0, resulting in the time-part to be zero.

If that is faster than the explicit CAST approach must be tested. The use of 0 suggests some implicit casting will be done in this expression.

这篇关于在mssql 2005中保存DateTime,不需要几分钟和几秒钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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