我有两列一个表@ABC as DateTime
和@xyz as DateTime
- 数据类型。
我想减去时间,我想这样做:
适用于ABC = 21/02/2012 6:10:00 PM
和XYZ = 01/01/2001 12:00:00 AM
- >第一排。
CONVERT(varchar(10), dbo.checkingtime.ABC – dbo.checkingtime.XYZ, 108)
我得到的结果为18:10
,但我希望结果只在小时和分钟内显示为05:50
。
有可能吗? ? ?
答案 0 :(得分:1)
这是你想要的东西。这是内置的功能,但我会建议你建立自己的。
DATEDIFF ( datepart , startdate , enddate )
请注意,您需要在同一天“屏蔽”开始日期和结束日期以获得您想要的结果(这是时间差异)。结果将是分钟,但您可以轻松地将其格式化为小时:分钟。
欢呼声
答案 1 :(得分:0)
您可以尝试这样的功能:
CREATE FUNCTION GetTimeDifference
(
@FirstDate datetime,
@SecondDate datetime
)
RETURNS varchar(10)
AS
BEGIN
DECLARE @Difference INT
DECLARE @FirstTimeInMin INT
DECLARE @SecondTimeInMin INT
SELECT @FirstTimeInMin =
(DATEPART(hour,@FirstDate) * 60 + DATEPART(minute,@FirstDate))
SELECT @SecondTimeInMin =
(DATEPART(hour,@SecondDate) * 60 + DATEPART(minute,@SecondDate))
IF @FirstTimeInMin = 0
SET @FirstTimeInMin = 24 * 60
IF @SecondTimeInMin = 0
SET @SecondTimeInMin = 24 * 60
SET @Difference = @FirstTimeInMin - @SecondTimeInMin
IF(@Difference < 0)
SET @Difference = @Difference * -1
RETURN RIGHT('0' + CONVERT(varchar(10), @Difference / 60), 2)
+ ':' +
RIGHT('0' + CONVERT(varchar(10), @Difference - (@Difference / 60) * 60 ), 2)
END
GO
你可以像这样使用它:
SELECT dbo.GetTimeDifference('02/02/2012 6:10:00 PM','01/01/2001 12:00:00 AM')
结果应该是05:50
答案 2 :(得分:0)
答案 3 :(得分:0)
这是我发现的好例子:
Select start_date, end_date, time_diff,
EXTRACT(DAY FROM time_diff) days,
EXTRACT(HOUR FROM time_diff) hours,
EXTRACT(MINUTE FROM time_diff) minutes,
EXTRACT(SECOND FROM time_diff) seconds
From
(
Select start_date, end_date, end_date - start_date time_diff
From
(
Select CAST(to_date('21/02/2012 06:10:00 am', 'dd/mm/yyyy hh:mi:ss am') AS TIMESTAMP) end_date
, CAST(to_date('01/01/2012 12:00:00 am', 'dd/mm/yyyy hh:mi:ss am') AS TIMESTAMP) start_date
From dual
))
/