SQL 如何在sql server中减去两次?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36906896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:35:19  来源:igfitidea点击:

how to subtract two time in sql server?

sqlsql-serversql-server-2012

提问by Mohammad Saadeh

So I don't know how to subtract two time(hh:mm:ss)in sql server.

所以我不知道如何time(hh:mm:ss)在sql server中减去两个。

This is my statement:

这是我的声明:

where   
 ( CONVERT(TIME(7), [EndWork], 102)) - ( CONVERT(TIME(7), [StartWork], 102)) <
 CONVERT(TIME(7), ' 8:30:00', 102)

采纳答案by Chanukya

DECLARE @END_DATE TIME = '' ,    
     @START_DATE  TIME = ''
     SELECT CONVERT(TIME,DATEADD(MS,DATEDIFF(SS, @START_DATE, @END_DATE )*1000,0),114)

回答by Abdul Rasheed

Using the DATEDIFF function you will get the difference of two dates/time in Year/Month/Day/Hour/Min/Sec as you required.

使用DATEDIFF函数,您将根据需要获得年/月/日/小时/分钟/秒中两个日期/时间的差异。

Eg:- DATEDIFF ( MINUTE , startdate , enddate )--will return the diff in minutes

例如:- --DATEDIFF ( MINUTE , startdate , enddate )将在几分钟内返回差异

回答by Rahul Tripathi

You can try to use the DATEDIFFfunction like this:

您可以尝试像这样使用DATEDIFF函数:

where DATEDIFF(HH,StartWork, EndWork)