sql server - Date Exists between Range of Dates not working -
alter procedure [dbo].[spinsert] (@planname varchar(50)=null ,@startdate datetime ,@enddate datetime ,@modifiedby varchar(100)=null ,@returnvalue int out) begin if not exists(select planname dbo.tblplan planname=@planname) begin if((select count(*) tblplan startdate <= @startdate , enddate <= @enddate)<0) begin insert dbo.tblplan values(3,@planname,@startdate,@enddate,@modifiedby,getdate(), (select datediff(dd,@startdate,@enddate))) set @returnvalue=1; end else set @returnvalue=-2; end else set @returnvalue=-1; end
i trying achieve below thing.i want check user supplied startdate , enddate in between existing table startdate , enddate. if of date of user supplied date range in between tables start date , end date,it should return -2,if record not exists should insert details..
i not achieve logic.where went wrong ..please suggest me solution this.
edit:first consditon check whether planname exists or not,if not exists want check start , end date existed or not(including start , end) tried 2 ways suggested mentioned in replies.
eg:if existing start , end range start-2013-10-09,end-2013-10-15,if going insert plan start , end date of plan should not fall between 9th-15th october , start , end date should not 9th or 15 th both.
one:if((select count(*) tblplan startdate <= @startdate , enddate <= @enddate)=0) result: not insert data, out of previous date. or in range second:if((select count(*) tblplan startdate>=@startdate , enddate<=@enddate)=0) result: insert date out considering above condition.
i think need change if from
if((select count(*) tblplan startdate <= @startdate , enddate <= @enddate)<0)
to
if((select count(*) tblplan startdate <= @startdate , enddate >= @enddate)=0)
which should ensure @startdate
, @enddate
between startdate
, enddate
, test =0
Comments
Post a Comment