如何查找两个日期之间的持续时间

编程入门 行业动态 更新时间:2024-10-24 06:30:56
本文介绍了如何查找两个日期之间的持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想查找两个日期列之间的持续时间。为此,我使用DATEDIFF函数分别查找号码年份和月份,但希望两个结果都列在单列中。 下面给出两列。

I want to find the duration between the two date columns. For this i used the DATEDIFF function to find number years and months separately but wanted both results in single column. The two columns are given below.

start_dt | end_dt 06-Oct-2009 15-Jul-2011

需要的结果

Duration(years.months) 2.3

推荐答案

我认为没有现成的API以您提到的格式提供结果。您需要使用 DATEDIFF 函数来获取所需最小面额的差额,然后将结果与适当的值进行分隔,以获得所需格式的持续时间。如下所示:

I think there is no out-of-the-box API to provide the result in the format you mentioned. You need to use the DATEDIFF function to get the difference in the least denomination you need and then divide the result with appropriate value to get the duration in the format required. Something like this:

DECLARE @start DATETIME DECLARE @end DATETIME DECLARE @duration INT SELECT @start = '2009-10-06', @end = '2011-07-15' SELECT @duration = DATEDIFF(mm, @start, @end) SELECT CONVERT(NVARCHAR, @duration / 12) + '.' + CONVERT(NVARCHAR, @duration % 12)

通过编写一个将采用日期和最小面额并以所需格式返回持续时间的函数可以更好地实现,因为它将需要TSQL和纯SQL不够。

This can be better achieved by writing a function that would take the dates and least denomination and returns the duration in the format needed, as it would require TSQL and plain SQL wouldn't suffice.

更多推荐

如何查找两个日期之间的持续时间

本文发布于:2023-10-27 15:33:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533802.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:持续时间   两个   日期

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!