如何比较两个日期以查找SQL Server 2005中的时差,日期操作


73

我有两列:

job_start                         job_end
2011-11-02 12:20:37.247           2011-11-02 13:35:14.613

如何使用T-SQL查找作业开始到结束之间经过的原始时间?

我尝试了这个:

select    (job_end - job_start) from tableA

但最终结果是:

1900-01-01 01:14:37.367

如您所见,DateTime减法的结果是另一个日期,等于1900.01.01加上所得的差,在您的情况下为1:14:37.367
Ant_222 '19

Answers:


126

看一下DateDiff()功能。

-- Syntax
-- DATEDIFF ( datepart , startdate , enddate )

-- Example usage
SELECT DATEDIFF(DAY, GETDATE(), GETDATE() + 1) AS DayDiff
SELECT DATEDIFF(MINUTE, GETDATE(), GETDATE() + 1) AS MinuteDiff
SELECT DATEDIFF(SECOND, GETDATE(), GETDATE() + 1) AS SecondDiff
SELECT DATEDIFF(WEEK, GETDATE(), GETDATE() + 1) AS WeekDiff
SELECT DATEDIFF(HOUR, GETDATE(), GETDATE() + 1) AS HourDiff
...

您可以在操作中看到它/在这里玩


19

您可以使用DATEDIFF函数获取分钟,秒,天等之间的差异。

SELECT DATEDIFF(MINUTE,job_start,job_end)

MINUTE显然以分钟为单位返回时差,您也可以使用DAY,HOUR,SECOND,YEAR(有关完整列表,请参见在线图书链接)。

如果您想花哨的话,可以用不同的方式显示,例如75分钟可以这样显示:01:15:00:0

这是针对SQL Server 2005和2008执行此操作的代码

-- SQL Server 2005
SELECT CONVERT(VARCHAR(10),DATEADD(MINUTE,DATEDIFF(MINUTE,job_start,job_end),'2011-01-01 00:00:00.000'),114)

-- SQL Server 2008
SELECT CAST(DATEADD(MINUTE,DATEDIFF(MINUTE,job_start,job_end),'2011-01-01 00:00:00.000') AS TIME)

1
@JamesHill谢谢James,您听起来好像需要休假!所以我赞成你的回答。
some_bloody_fool 2012年

@some_bloody_fool,((:谢谢。您实际上可能是对的!
James Hill

Ok Ive添加了另一种方式,供您选择显示。
文斯Pergolizzi 2012年

12

将结果强制转换为TIME,结果将以时间格式显示间隔的持续时间。

select CAST(job_end - job_start) AS TIME(0)) from tableA

2
太好了,这就是我要寻找的东西。谢谢!
hakuna

5

我认为您需要job_start和job_end之间的时间间隔。

尝试这个...

select SUBSTRING(CONVERT(VARCHAR(20),(job_end - job_start),120),12,8) from tableA

我结束了这个。

01:14:37

2

声明开始和结束日期 DECLARE @SDATE AS DATETIME

TART_DATE  AS DATETIME
DECLARE @END_-- Set Start and End date
SET @START_DATE = GETDATE()
SET @END_DATE    = DATEADD(SECOND, 3910, GETDATE())

-以HH:MI:SS:MMM(24H)格式获取结果 SELECT CONVERT(VARCHAR(12), DATEADD(MS, DATEDIFF(MS, @START_DATE, @END_DATE), 0), 114) AS TimeDiff


0

在Sql Server中尝试

SELECT 
      start_date as firstdate,end_date as seconddate
       ,cast(datediff(MI,start_date,end_date)as decimal(10,3)) as minutediff
      ,cast(cast(cast(datediff(MI,start_date,end_date)as decimal(10,3)) / (24*60) as int ) as varchar(10)) + ' ' + 'Days' + ' ' 
      + cast(cast((cast(datediff(MI,start_date,end_date)as decimal(10,3)) / (24*60) - 
        floor(cast(datediff(MI,start_date,end_date)as decimal(10,3)) / (24*60)) ) * 24 as int) as varchar(10)) + ':' 

     + cast( cast(((cast(datediff(MI,start_date,end_date)as decimal(10,3)) / (24*60) 
      - floor(cast(datediff(MI,start_date,end_date)as decimal(10,3)) / (24*60)))*24
        -
        cast(floor((cast(datediff(MI,start_date,end_date)as decimal(10,3)) / (24*60) 
      - floor(cast(datediff(MI,start_date,end_date)as decimal(10,3)) / (24*60)))*24) as decimal)) * 60 as int) as varchar(10))

    FROM [AdventureWorks2012].dbo.learndate

0

如果您的数据库StartTime = 07:00:00和endtime = 14:00:00,并且都是时间类型。您的查询以获取时差为:

SELECT TIMEDIFF(Time(endtime ), Time(StartTime )) from tbl_name

如果数据库startDate =2014-07-20 07:00:00和endtime = 2014-07-20 23:00:00,则也可以使用此查询。


哪个版本的SQL Server支持TIMEDIFF功能?
亚历克斯

0

下面的代码以hh:mm格式给出。

选择RIGHT(LEFT(job_end- job_start,17),5)


1
我认为这并非OP所要求的。使用DATE_DIFF应该会有所帮助...
Tyron78 '17

0

看看DATEDIFF,这应该是您想要的。它需要您比较的两个日期,以及您想要的差异的日期单位(天,月,秒...)



0

如果您尝试以一定的准确性获得工作时间,请尝试使用此方法(已在SQL Server 2016中测试)

SELECT DATEDIFF(MINUTE,job_start, job_end)/60.00;

各种DATEDIFF功能是:

SELECT DATEDIFF(year,        '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
SELECT DATEDIFF(quarter,     '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
SELECT DATEDIFF(month,       '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
SELECT DATEDIFF(dayofyear,   '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
SELECT DATEDIFF(day,         '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
SELECT DATEDIFF(week,        '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
SELECT DATEDIFF(hour,        '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
SELECT DATEDIFF(minute,      '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
SELECT DATEDIFF(second,      '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
SELECT DATEDIFF(millisecond, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');

参考:https : //docs.microsoft.com/zh-cn/sql/t-sql/functions/datediff-transact-sql? view = sql-server- 2017

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.