sql server中两个日期时间的差异 [英] Difference of two date time in sql server

查看:144
本文介绍了sql server中两个日期时间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有没有办法在sql server中使用两个 datetime 之间的区别?例如,我的日期是


  1. 2010-01-22 15:29:55.090

  2. 2010-01-22 15:30:09.153

所以,结果应该是$ code> 14.063秒。

解决方案

只需添加一个关于DateDiff的警告,它会计算您指定为单位的边界次数,因此如果您正在寻找精确的时间间隔,则会遇到问题。
例如

  select datediff(m,'20100131','20100201')

给出答案1,因为它跨越了1月到2月的边界,所以即使跨度是2天,datediff将返回价值1 - 它越过1个日期边界。

  select datediff(mi,'2010-01-22 15:29:55.090 ','2010-01-22 15:30:09.153')

赋值为1,再次,它通过分钟边界一次,所以即使是大约14秒,它将返回一分钟,使用分钟作为单位。


Is there any way to take the difference between two datetime in sql server?

For example, my dates are

  1. 2010-01-22 15:29:55.090
  2. 2010-01-22 15:30:09.153

So, the result should be 14.063 seconds.

解决方案

Just a caveat to add about DateDiff, it counts the number of times you pass the boundary you specify as your units, so is subject to problems if you are looking for a precise timespan. e.g.

select datediff (m, '20100131', '20100201')

gives an answer of 1, because it crossed the boundary from January to February, so even though the span is 2 days, datediff would return a value of 1 - it crossed 1 date boundary.

select datediff(mi, '2010-01-22 15:29:55.090' , '2010-01-22 15:30:09.153')

Gives a value of 1, again, it passed the minute boundary once, so even though it is approx 14 seconds, it would be returned as a single minute when using Minutes as the units.

这篇关于sql server中两个日期时间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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