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

时间:2012-03-01 17:59:27

标签: sql sql-server-2005 date datediff

我有两列:

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

11 个答案:

答案 0 :(得分:121)

查看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
...

您可以在行动中看到它/ play with it here

答案 1 :(得分: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)

答案 2 :(得分:12)

将结果转换为TIME,结果将采用时间格式表示间隔时间。

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

答案 3 :(得分:5)

我认为你需要 job_start&的时间差距。 job_end

试试这个......

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

我最终得到了这个。

01:14:37

答案 4 :(得分: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

答案 5 :(得分:0)

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

答案 6 :(得分:0)

如果您的数据库StartTime = 07:00:00和结束时间= 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,您也可以使用此查询。

答案 7 :(得分: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

答案 8 :(得分:0)

下面的代码以hh:mm格式提供。

选择右(左(job_end- job_start,17),5)

答案 9 :(得分:0)

我使用了以下逻辑,它像奇迹一样对我有用:

CONVERT(TIME, DATEADD(MINUTE, DATEDIFF(MINUTE, AP.Time_IN, AP.Time_OUT), 0)) 

答案 10 :(得分: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/en-us/sql/t-sql/functions/datediff-transact-sql?view=sql-server-2017