我已经使用日期和时间验证来安排报告……我必须只为将来的日期和时间而不是以前的日期和时间安排报告……我已经使用了这个
declare @Dt varchar(50)
declare @Hr varchar(50)
declare @trandate_time_tmp as TIME(0)
select @trandate_time_tmp = getdate()
set @Dt = DATEDIFF (D,@schedule_date ,@trandate_tmp )
set @Hr = DATEDIFF (S,@schedule_date ,@trandate_time_tmp )
if ( @Dt > 0)
begin
raiserror('Schedule Date should not be earlier than system date',16,1)
return
end
if ( @Hr > 0)
begin
raiserror('Schedule Time should not be earlier than system time',16,1)
return
end
对于日期部分,它正在正确检查,但对于时间,它抛出错误
The datediff function resulted in an overflow. The number of dateparts separating two date/time instances is too large. Try to use datediff with a less precise datepart.
最佳答案
不完全回答您的问题,但可能是您问题的解决方案。您不需要使用 DATEDIFF 并检查结果,您只需比较两个日期即可。
IF ( @schedule_date <= GETDATE() )
BEGIN
RAISERROR('Schedule date should not be earlier than system date', 16, 1)
RETURN
END
关于sql - sql中的日期时间比较错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4953912/