我有一个具有“时间”列的SQL Server表。该表是一个日志表,其中包含每条消息的状态消息和时间戳。日志表通过批处理文件插入。有一个ID列可以将行组合在一起。每次批处理文件运行时,它都会初始化ID并写入记录。我需要做的是从ID集中的第一条记录到同一ID集的最后一条记录的经过时间。我从logTable中开始选择Max(Time) - Min(Time),其中id =但无法弄清楚如何正确格式化。我需要它在HH:MM:SS。
答案 0 :(得分:13)
<强>更新:强>
正确计算SQL Server中的时间跨度,即使超过24小时:
-- Setup test data
declare @minDate datetime = '2012-12-12 20:16:47.160'
declare @maxDate datetime = '2012-12-13 15:10:12.050'
-- Get timespan in hh:mi:ss
select cast(
(cast(cast(@maxDate as float) - cast(@minDate as float) as int) * 24) /* hours over 24 */
+ datepart(hh, @maxDate - @minDate) /* hours */
as varchar(10))
+ ':' + right('0' + cast(datepart(mi, @maxDate - @minDate) as varchar(2)), 2) /* minutes */
+ ':' + right('0' + cast(datepart(ss, @maxDate - @minDate) as varchar(2)), 2) /* seconds */
-- Returns 18:53:24
特别欢迎显示不准确的边缘情况!
答案 1 :(得分:9)
SQL Server不支持SQL标准间隔数据类型。您最好的选择是以秒为单位计算差异,并使用函数格式化结果。只要您的间隔小于24小时,本机函数CONVERT()似乎可以正常工作。但是CONVERT()不是一个很好的解决方案。
create table test (
id integer not null,
ts datetime not null
);
insert into test values (1, '2012-01-01 08:00');
insert into test values (1, '2012-01-01 09:00');
insert into test values (1, '2012-01-01 08:30');
insert into test values (2, '2012-01-01 08:30');
insert into test values (2, '2012-01-01 10:30');
insert into test values (2, '2012-01-01 09:00');
insert into test values (3, '2012-01-01 09:00');
insert into test values (3, '2012-01-02 12:00');
以
的方式选择值此SELECT语句包括一列计算秒数,另一列使用带减法的CONVERT()。
select t.id,
min(ts) start_time,
max(ts) end_time,
datediff(second, min(ts),max(ts)) elapsed_sec,
convert(varchar, max(ts) - min(ts), 108) do_not_use
from test t
group by t.id;
ID START_TIME END_TIME ELAPSED_SEC DO_NOT_USE
1 January, 01 2012 08:00:00 January, 01 2012 09:00:00 3600 01:00:00
2 January, 01 2012 08:30:00 January, 01 2012 10:30:00 7200 02:00:00
3 January, 01 2012 09:00:00 January, 02 2012 12:00:00 97200 03:00:00
请注意,对于身份证号码3的27小时差异,会产生误导性的“03:00:00”。
答案 2 :(得分:4)
DECLARE @EndTime AS DATETIME, @StartTime AS DATETIME
SELECT @StartTime = '2013-03-08 08:00:00', @EndTime = '2013-03-08 08:30:00'
SELECT CAST(@EndTime - @StartTime AS TIME)
结果:00:30:00.0000000
根据需要格式化结果。
答案 3 :(得分:1)
看看这是否有帮助。我可以为Elapsed Days,Hours,Minutes,Seconds设置变量。 您可以根据自己的喜好对其进行格式化,也可以将其包含在用户定义的函数中。
注意:不要使用DateDiff(hh,@ Date1,@ Date2)。这不可靠!它以不可预测的方式进行巡视
鉴于两个日期...... (样本日期:两天,三小时,10分钟,30秒差异)
declare @Date1 datetime = '2013-03-08 08:00:00'
declare @Date2 datetime = '2013-03-10 11:10:30'
declare @Days decimal
declare @Hours decimal
declare @Minutes decimal
declare @Seconds decimal
select @Days = DATEDIFF(ss,@Date1,@Date2)/60/60/24 --Days
declare @RemainderDate as datetime = @Date2 - @Days
select @Hours = datediff(ss, @Date1, @RemainderDate)/60/60 --Hours
set @RemainderDate = @RemainderDate - (@Hours/24.0)
select @Minutes = datediff(ss, @Date1, @RemainderDate)/60 --Minutes
set @RemainderDate = @RemainderDate - (@Minutes/24.0/60)
select @Seconds = DATEDIFF(SS, @Date1, @RemainderDate)
select @Days as ElapsedDays, @Hours as ElapsedHours, @Minutes as ElapsedMinutes, @Seconds as ElapsedSeconds
答案 4 :(得分:1)
最简单的方法:
Convert(varchar, {EndTime} - {StartTime}, 108)
就像Anri所说的那样。
答案 5 :(得分:1)
使用DATEDIFF返回以毫秒,秒,分钟,小时为单位的值...
DATEDIFF(interval,date1,date2)
间隔 需要 - 要返回的时间/日期部分。可以是以下值之一:
year, yyyy, yy = Year
quarter, qq, q = Quarter
month, mm, m = month
dayofyear = Day of the year
day, dy, y = Day
week, ww, wk = Week
weekday, dw, w = Weekday
hour, hh = hour
minute, mi, n = Minute
second, ss, s = Second
millisecond, ms = Millisecond
date1,date2 REQUIRED - 计算差异的两个日期
答案 6 :(得分:0)
select convert(varchar, Max(Time) - Min(Time) , 108) from logTable where id=...
答案 7 :(得分:0)
希望这可以帮助您获得两个时间戳之间的确切时间
Create PROC TimeDurationbetween2times(@iTime as time,@oTime as time)
As
Begin
DECLARE @Dh int, @Dm int, @Ds int ,@Im int, @Om int, @Is int,@Os int
SET @Im=DATEPART(MI,@iTime)
SET @Om=DATEPART(MI,@oTime)
SET @Is=DATEPART(SS,@iTime)
SET @Os=DATEPART(SS,@oTime)
SET @Dh=DATEDIFF(hh,@iTime,@oTime)
SET @Dm = DATEDIFF(mi,@iTime,@oTime)
SET @Ds = DATEDIFF(ss,@iTime,@oTime)
DECLARE @HH as int, @MI as int, @SS as int
if(@Im>@Om)
begin
SET @Dh=@Dh-1
end
if(@Is>@Os)
begin
SET @Dm=@Dm-1
end
SET @HH = @Dh
SET @MI = @Dm-(60*@HH)
SET @SS = @Ds-(60*@Dm)
DECLARE @hrsWkd as varchar(8)
SET @hrsWkd = cast(@HH as char(2))+':'+cast(@MI as char(2))+':'+cast(@SS as char(2))
select @hrsWkd as TimeDuration
End