我有两列:
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
我有两列:
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
Answers:
看一下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
...
您可以在操作中看到它/在这里玩
您可以使用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)
我认为您需要job_start和job_end之间的时间间隔。
尝试这个...
select SUBSTRING(CONVERT(VARCHAR(20),(job_end - job_start),120),12,8) from tableA
我结束了这个。
01:14:37
声明开始和结束日期
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
在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
如果您的数据库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
,则也可以使用此查询。
TIMEDIFF
功能?
下面的代码以hh:mm格式给出。
选择RIGHT(LEFT(job_end- job_start,17),5)
我使用以下逻辑,对我来说就像奇迹一样有效:
CONVERT(TIME, DATEADD(MINUTE, DATEDIFF(MINUTE, AP.Time_IN, AP.Time_OUT), 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
DateTime
减法的结果是另一个日期,等于1900.01.01加上所得的差,在您的情况下为1:14:37.367。