到期日=' 2017-10-16'和ExpiryTime =' 12:00 pm'在表格和我们国家,Getdate是2017-10-16'和currentdatetime是' 2017-10-16 11:05:33.503'
但是,我的代码仍然执行IF条件。为什么?
Declare @ExpiryDate date
Declare @ExpiryTime varchar(10)
Set @ExpiryDate= (Select convert(varchar(11), ExpiryDate, 106) from Works where NIT_No= @NITNo and WorkNo= @WorkNo)
Set @ExpiryTime= (Select CAST(ExpiryTime as TIME(0)) from Works where NIT_No= @NITNo and WorkNo= @WorkNo)
IF(CONVERT(DATETIME,CONVERT(VARCHAR,@ExpiryDate,106)+ ' ' + @ExpiryTime) <= CONVERT(datetime, GETDATE()))
Begin
RAISERROR('Sorry, Expiry date and time has passed', 16, 10);
return;
End
答案 0 :(得分:2)
12:00 pm以24小时格式翻译为00:00。如果你把当前日期和中午12点结合起来,你希望结果是第二天的午夜,但实际上是你当天的午夜。
这应该有效:
Declare @ExpiryDate date
Declare @ExpiryTime varchar(10)
Set @ExpiryDate= (Select convert(varchar(11), ExpiryDate, 106) from Works where NIT_No= @NITNo and WorkNo= @WorkNo)
Set @ExpiryTime= (Select CAST(ExpiryTime as TIME(0)) from Works where NIT_No= @NITNo and WorkNo= @WorkNo)
declare @dateTimeCombined datetime = dateadd(ms, datediff(ms, '00:00:00', @ExpiryTime), cast(@ExpiryDate as datetime))
IF @dateTimeCombined <= CONVERT(datetime, GETDATE())
Begin
RAISERROR('Sorry, Expiry date and time has passed', 16, 10);
return;
End