本文介绍了得到两个日期或两个小时之间的差,然后插入它们,否则不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表Events中有2个字段(Game_Date,Hour_Game).这些字段以Varchar(50)作为数据类型,我不想将它们的Datetime或time作为数据类型传递给我,我想将它们保留为Varchar(50) ),然后我要计算输入的日期与表中已经存在的日期之间的差值(如果插入的日期大于1天,则应插入该日期,否则不插入);如果小时数大于2,则与小时相同在执行SP时未插入,即使日期大于1或小时大于2小时也使我进入了else条件,这是我正在使用的SP, :

Hi I have 2 fields(Game_Date,Hour_Game) in my table Events.Those field has Varchar(50) as Datatype and I dont want to pass them Datetime or time as data type.I want to keep them as Varchar(50),Then I want to take the difference between the date entered and the date that already exists in the table if the date inserted is greater than 1 day the date should be inserted else not,same for the hour if the hour is greater than 2 hours it should be inserted else not.When executing the SP it is not inserted,It took me into the else condition even if the date is greater than 1 or the hour is greater than 2 hours, this is the SP I am using and It :

ALTER PROC [dbo].[InsertData]
@Ev VARCHAR(50)='',
@idstadium VARCHAR(50)='',
@GameDate VARCHAR(50)='',
@GameHour VARCHAR(50)='',
@msg VARCHAR(4000)=NULL OUTPUT
AS
BEGIN
DECLARE @i AS INT
DECLARE @DateDiff AS VARCHAR(50)
DECLARE @DateHour AS VARCHAR(50)


SET @DateDiff=(SELECT DATEDIFF(day,Game_Date,@GameDate) FROM dbo.Events WHERE EventsID=@Ev)
SET @DateHour=(SELECT DATEDIFF(hour,Hour_Game,@GameHour) FROM dbo.Events WHERE EventsID=@Ev)

          IF (@DateDiff>1 and @DateHour>2)
          BEGIN
          INSERT INTO dbo.Events(EventsID,Id_Stadium,Game_Date,Hour_Game)
          VALUES(@Ev,@idstadium,@GameDate,@GameHour)
          SET @msg='Data inserted!'
          END
          ELSE
          BEGIN
           SET @msg='Hour should be greater than 2 or Date should be greater than 1'
         END

END

帮助解决此问题...

Help fixing this...

推荐答案



这篇关于得到两个日期或两个小时之间的差,然后插入它们,否则不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 00:48