我想找到两个日期列之间的持续时间。为此,我使用 DATEDIFF 函数分别查找数字年份和月份,但希望两个结果都在单列中。下面给出了这两列。
start_dt | end_dt
06-Oct-2009 15-Jul-2011
需要的结果
Duration(years.months)
2.3
我想找到两个日期列之间的持续时间。为此,我使用 DATEDIFF 函数分别查找数字年份和月份,但希望两个结果都在单列中。下面给出了这两列。
start_dt | end_dt
06-Oct-2009 15-Jul-2011
需要的结果
Duration(years.months)
2.3
我认为没有开箱即用的 API 可以以您提到的格式提供结果。您需要使用该DATEDIFF
函数来获得所需的最小面额的差异,然后将结果除以适当的值以获得所需格式的持续时间。像这样的东西:
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 是不够的。
尝试这样的事情
SELECT
DATEDIFF( mm, start_dt, end_dt) / 12 AS years
, DATEDIFF( mm, start_dt, end_dt) % 12 AS months
询问 :
SELECT
CONVERT(VARCHAR, DATEADD(month, 1, GETDATE()), 103) AS FinalDay,
CONVERT(VARCHAR, HRM_EmpGenDtls.DOJ, 106) AS DOJ,
CONVERT(varchar, DATEDIFF(year, HRM_EmpGenDtls.DOJ, GETDATE())) + ' Years' + ' ' +
CONVERT(varchar, DATEDIFF(month, HRM_EmpGenDtls.DOJ, GETDATE()) - (datediff(year, HRM_EmpGenDtls.DOJ, GETDATE()) * 12)) + ' Months' + ' ' + CONVERT(varchar, datepart(d,GETDATE()) - datepart(d, HRM_EmpGenDtls.DOJ)) + ' Days' AS ServicePeriod
FROM HRM_EmpGenDtls
这给出了以下答案:
FinalDay DOJ ServicePeriod
10/10/2013 03 Sep 2012 1 Years 0 Months 7 Days
10/10/2013 01 Jan 2000 13 Years 8 Months 9 Days
10/10/2013 22 Aug 2000 13 Years 1 Months -12 Days
10/10/2013 22 Aug 2000 13 Years 1 Months -12 Days
10/10/2013 22 Aug 2000 13 Years 1 Months -12 Days
10/10/2013 22 Aug 2000 13 Years 1 Months -12 Days
10/10/2013 22 Aug 2000 13 Years 1 Months -12 Days
10/10/2013 04 Sep 2012 1 Years 0 Months 6 Days
SELECT (DATEDIFF(month, start_dt, end_dt) / 12);
试试这个。它可能会有所帮助
select cast (DATEDIFF(m,'06-Oct-2009','15-Jul-2011')/12 as varchar(25)) + '.' + cast (DATEDIFF(m,'06-Oct-2009','15-Jul-2011')%12 as varchar(25))
我试过这个-
SELECT
CONVERT(varchar,DATEDIFF(MONTH,SAKUSEITIME,KOSHINTIME)/12)+'.'
+CONVERT(varchar,DATEDIFF(MONTH,SAKUSEITIME,KOSHINTIME)%12) Duration
FROM MYTABLE
这是我查找两个日期之间时间差的函数。
ALTER FUNCTION functionName
-- The first imput is the start time and the second input is the end time.
@StartTime datetime, @EndTime datetime
RETURNS varchar(15)
AS
BEGIN
DECLARE @Duration varchar(15)
SET @Duration =
convert(nvarchar(10),(COALESCE (DATEDIFF(day,@StartTime,@EndTime),DATEDIFF(day,@StartTime,CURRENT_TIMESTAMP)))) + '.' +
right('00'+ISNULL(convert(nvarchar(10),(COALESCE (DATEDIFF(hour,@StartTime,@EndTime)%24,DATEDIFF(hour,@StartTime,CURRENT_TIMESTAMP)%24))),' '),2) + ':' +
right('00'+ISNULL(convert(nvarchar(10),(COALESCE (DATEDIFF(minute,@StartTime,@EndTime)%60,DATEDIFF(minute,@StartTime,CURRENT_TIMESTAMP)%60))),' '),2) + ':' +
right('00'+ISNULL(convert(nvarchar(10),(COALESCE (DATEDIFF(second,@StartTime,@EndTime)%60,DATEDIFF(second,@StartTime,CURRENT_TIMESTAMP)%60))),' '),2)
-- Calculates the difference in each until of time, finds the remainder, and add the pieces of the timestamp together.
RETURN @Duration
END
declare @StartDate datetime, @EndDate datetime
select @StartDate = '2016-08-24 14:44:54.513',@EndDate='2016-08-24 14:48:16.173'
Select convert(varchar,CAST((@EndDate-@StartDate) as time(0))) '[hh:mm:ss]'