任何人都可以告诉我如何以人类可读的形式格式化给定的秒数(例如16742秒)(例如1d 5h 2m 13s)
答案 0 :(得分:1)
我个人会为此制作一个TSQL函数。以下是您可以调整的示例:
IF(OBJECT_ID('FN_STRING_TO_TIME', 'FN') IS NOT NULL)
DROP FUNCTION FN_STRING_TO_TIME
GO
CREATE FUNCTION dbo.FN_STRING_TO_TIME(
-- Seconds to convert
@pSeconds INT
)
RETURNS VARCHAR(12)
---------------------------------------------------------------------------------------
-- Developer: Linus Brimstedt
-- Date: 2009-03-17
--
-- Function: Returns the given seconds in H:MM:SS format
--
-- Output: String in format H:MM:SS
--
---------------------------------------------------------------------------------------
BEGIN
-----------------------------
-- Variables
-----------------------------
DECLARE @output VARCHAR(30) -- Describe the variables
DECLARE @minutes INT
DECLARE @hours INT
-----------------------------
-- Implementation
-----------------------------
SET @minutes = @pSeconds / 60
SET @pSeconds = @pSeconds % 60
SET @hours = @minutes / 60
SET @minutes = @minutes % 60
-----------------------------
-- Return output
-----------------------------
RETURN CAST(@hours AS VARCHAR) + ':'
+ dbo.FN_STRING_LPAD(@minutes, 2, '0') + ':'
+ dbo.FN_STRING_LPAD(@pSeconds, 2, '0')
END
GO
-- Test
DECLARE @seconds INT
, @got VARCHAR(12)
, @expected VARCHAR(12)
SELECT @seconds = 67
, @expected = '0:01:07'
SET @got = dbo.FN_STRING_TO_TIME(@seconds)
IF(@got != @expected)
RAISERROR('FN_STRING_TO_TIME(%d) returned bad value: Expected: %s, got %s', 11, 11, @seconds, @expected, @got)
SELECT @seconds = 60 * 60 * 7 + 60 * 14 + 34
, @expected = '7:14:34'
SET @got = dbo.FN_STRING_TO_TIME(@seconds)
IF(@got != @expected)
RAISERROR('FN_STRING_TO_TIME(%d) returned bad value: Expected: %s, got %s', 11, 11, @seconds, @expected, @got)
干杯 / L
答案 1 :(得分:1)
我使用Deski和四个变量完成了它。 从小时开始测量,创建一些变量,在下面我的年变量是vYears,我的Month变量是vMonths,我的days变量是vDays,我的hours变量是vHours。
vYears使用以下计算:
=Floor(Sum(<Hours Billed by Date Billed>/24)/365.25)
vWeeks使用以下计算:
=Floor(Sum((Sum(<Hours Billed by Date Billed>/24)/365.25)-<vYears>)*365.25/7)
vDays使用以下计算:
=Floor(Sum(Sum((Sum(<Hours Billed by Date Billed>/24)/365.25)-<vYears>)*365.25/7-<vWeeks>)*52/24)
vHours使用以下计算:
=Truncate(Sum((Sum(Sum((Sum(<Hours Billed by Date Billed>/24)/365)-<vYears>)*365/7-<vWeeks>)*52/24-<vDays>)*24) , 2)
I然后可以通过执行以下操作输出这些变量:
=<vYears>+" Years "+<vWeeks>+" Weeks "+<vDays>+" Days "+<vHours>+" Hours"
如果您需要进一步的信息,请不要犹豫。
谢谢,
马特