计算startTime和endTime之间的分钟差异

时间:2020-12-22 19:16:32

I have to calculate the difference from StartTime and EndTime. If the EndTime is less then 15 minutes that of StartTime I have to show an error.

我必须计算StartTime和EndTime的差异。如果EndTime小于StartTime的15分钟,我必须显示错误。

CREATE PROCEDURE [Travel].[TravelRequirementValidate] 
@Action char(1)
,@CallId int
,@PhaseId int
,@ShipId int 
,@CallStartDate datetime
,@CallEndDate DATETIME
,@CallStartTime datetime 
,@CallEndTime datetime   
,@LanguageId int
,@SessionGroup nvarchar(100)
,@SessionPlace nvarchar(100)
,@ActiveFlg tinyint
,@WarningMessage nvarchar(300)=NULL output
,@Minutes int 
as
if @Action in ('I','U')
begin
   @Minutes=select DATEDIFF(@CallStartDate,@CallStartTime,@CallEndTime) from [Travel].[TravelRequirement] 
if @Minutes<=15
begin
  raiserror(3,11,1) --CallEndTime must be equals or higher than 15 minutes
  return
end
end

This code doesn't work. I've got an error for the first parameter of DATEDIFF (invalid parameter 1 specified for datediff).

此代码不起作用。我有DATEDIFF的第一个参数错误(为datediff指定的参数1无效)。

How can I fix my code?

我该如何修复我的代码?

EDIT

编辑

I changed @Minutes=select DATEDIFF(@CallStartDate,@CallStartTime,@CallEndTime) from [Travel].[TravelRequirement]

我改变了@minutes =从[Travel]中选择DATEDIFF(@ CallStartDate,@ CallStartTime,@ CallEndTime)。[TravelRequirement]

in

declare @Diff int
@Diff=select DATEDIFF(@Minutes,@CallStartTime,@CallEndTime) from [Travel].[TravelRequirement] 

but I have the same error

但我有同样的错误

3 个解决方案

#1


2  

Function DATEDIFF wants as first parameter the portion of time.

函数DATEDIFF希望将时间部分作为第一个参数。

The correct use of it is:

正确使用它是:

DATEDIFF(minute, @CallStartTime, @CallEndTime)

#2


1  

The correct syntax for that expression would be:

该表达式的正确语法是:

select @Minutes = datediff(minute, @CallStartTime, @CallEndTime)
from [Travel].[TravelRequirement];

However, this does't make sense, because -- presumably -- the table has more than one row. Which row do you want?

但是,这没有意义,因为 - 据推测 - 该表有多行。你想要哪一排?

#3


1  

I would suggest using seconds and devide by 60.0 - provides a more accurate result:

我建议使用秒和devide 60.0 - 提供更准确的结果:

select @Minutes = datediff(second, @CallStartTime, @CallEndTime)/60.0
from [Travel].[TravelRequirement];

#1


2  

Function DATEDIFF wants as first parameter the portion of time.

函数DATEDIFF希望将时间部分作为第一个参数。

The correct use of it is:

正确使用它是:

DATEDIFF(minute, @CallStartTime, @CallEndTime)

#2


1  

The correct syntax for that expression would be:

该表达式的正确语法是:

select @Minutes = datediff(minute, @CallStartTime, @CallEndTime)
from [Travel].[TravelRequirement];

However, this does't make sense, because -- presumably -- the table has more than one row. Which row do you want?

但是,这没有意义,因为 - 据推测 - 该表有多行。你想要哪一排?

#3


1  

I would suggest using seconds and devide by 60.0 - provides a more accurate result:

我建议使用秒和devide 60.0 - 提供更准确的结果:

select @Minutes = datediff(second, @CallStartTime, @CallEndTime)/60.0
from [Travel].[TravelRequirement];