2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SQLで日時の経過時間(hh:mm:ss)を抽出する

Last updated at Posted at 2019-04-17

SQLServerで2つの日付の経過時間を抽出するには、以下のSQLで抽出できます。
処理に掛かった時間(タイムスパン)を求めたい時などに使えます。

SQL


DECLARE @出勤時間 DATETIME = '2019-04-17 08:45:23.900'
DECLARE @終了時間 DATETIME = '2019-04-17 18:00:25.080'

SELECT                  
 @出勤時間 AS 出勤時間 
,@終了時間 AS 終了時間 
,CONVERT(CHAR(12), 
	DATEADD(SECOND, 
		DATEDIFF(SECOND, @出勤時間, @終了時間), CONVERT(DATETIME, 0)), 108) AS 経過時間
,CONVERT(CHAR(12), 
	DATEADD(MILLISECOND, 
		DATEDIFF(MILLISECOND, @出勤時間, @終了時間), CONVERT(DATETIME, 0)), 114) AS [経過時間(ミリ秒)];

抽出結果

出勤時間 退勤時間 勤務時間 勤務時間(ミリ秒)
2019-04-17 15:49:23.900 2019-04-17 15:50:25.080 00:01:02 00:01:01:180
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?