SQL Server - 计算HH:MM:SS格式的两个日期时间戳之间的经过时间 [英] SQL Server - calculate elapsed time between two datetime stamps in HH:MM:SS format

查看:135
本文介绍了SQL Server - 计算HH:MM:SS格式的两个日期时间戳之间的经过时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQL Server表具有时间列。该表是每个消息的房屋状态消息和时间戳的日志表。日志表通过批处理文件插入。有一个ID列将行分组在一起。每次运行批处理文件时,它将初始化ID并写入记录。我需要做的是将ID设置中的第一个记录的经过时间从同一个ID集的最后一个记录中获取。我开始用select(最大)(Time) - 最小(时间)从logTable,其中id =但不知道如何格式化它正确。我需要它在HH:MM:SS。

解决方案

SQL Server不支持SQL标准间隔数据类型。最好的办法是用秒计算差值,并使用一个函数对结果进行格式化。只要您的间隔小于24小时,本机函数CONVERT()可能会显示为正常工作。但是,CONVERT()不是一个很好的解决方案。

  create table test(
id integer not null,
ts datetime not null
) ;

插入测试值(1,'2012-01-01 08:00');
插入测试值(1,'2012-01-01 09:00');
插入测试值(1,'2012-01-01 08:30');
插入测试值(2,'2012-01-01 08:30');
插入测试值(2,'2012-01-01 10:30');
插入测试值(2,'2012-01-01 09:00');
插入测试值(3,'2012-01-01 09:00');
插入测试值(3,'2012-01-02 12:00');

以这样的方式选择了价值,即




  • id = 1,已用时间为1小时

  • id = 2,经过时间为2小时,

  • id = 3,经过时间为3小时。



此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由t.id测试t


ID START_TIME END_TIME ELAPSED_SEC DO_NOT_USE
2012年1月1日08:00:00 2012年1月01日09:00:00 3600 01:00:00
01月01日2012 08:30:00 2012年1月01日10:30:00 7200 02:00:00
2012年1月3日09:00:00 2012年1月02日12:00:00 97200 03:00:00

请注意,对于ID号3的27小时差异,请注意误导03:00:00。



在SQL Server中格式化经过时间的功能


I have a SQL Server table that has a "Time" column. The table is a log table the houses status messages and timestamps for each message. The log table is inserted into via a batch file. There is an ID column that groups rows together. Each time the batch file runs it initializes the ID and writes records. What I need to do is get the elapsed time from the first record in an ID set to the last record of the same ID set. I started toying with select Max(Time) - Min(Time) from logTable where id = but couldn't figure out how to format it correctly. I need it in HH:MM:SS.

解决方案

SQL Server doesn't support the SQL standard interval data type. Your best bet is to calculate the difference in seconds, and use a function to format the result. The native function CONVERT() might appear to work fine as long as your interval is less than 24 hours. But CONVERT() isn't a good solution for this.

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');

Values were chosen in such a way that for

  • id = 1, elapsed time is 1 hour
  • id = 2, elapsed time is 2 hours, and
  • id = 3, elapsed time is 3 hours.

This SELECT statement includes one column that calculates seconds, and one that uses CONVERT() with subtraction.

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

Note the misleading "03:00:00" for the 27-hour difference on id number 3.

Function to format elapsed time in SQL Server

这篇关于SQL Server - 计算HH:MM:SS格式的两个日期时间戳之间的经过时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆