Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
i have a procedure and i want to check whether the date entered by user should not be greater then current day and not less then 3 yearsso i am checking year month and days by matching with IF statement.now when i m trying to run then i m getting error of nesting of if .can anyone help. and send me corrent method.the SQL sode is below.:alter proc datedifference@enterdate datetimeasdeclare @currentdate datetimeset @currentdate=getdate()--declare @enterdate datetime--set @enterdate='02/09/2007'declare @currentyear intdeclare @enteryear intdeclare @subscurrentyear intdeclare @result intset @result=0declare @currentday intset @currentday=day(@currentdate)declare @currentmonth intset @currentmonth=month(@currentdate)declare @entermonth intset @entermonth=month(@enterdate)declare @enterday intset @enterday=day(@enterdate)set @currentyear=year(@currentdate)set @subscurrentyear=@currentyear-3set @enteryear=year(@enterdate) if(@enteryear<=@currentyear and @enteryear>=@subscurrentyear) begin if( @entermonth<=@currentmonth) begin if(@enterday<@currentday) begin print 'valid date' end end endelseprint 'invalid date'scoo
Lamprey
Master Smack Fu Yak Hacker
4614 Posts
Posted - 2010-07-02 : 13:32:16
Something like this?
DECLARE @EnterDate DATETIMEDECLARE @CurrentDate DATETIME--SET @EnterDate = '20070902'SET @EnterDate = '20070601'SET @CurrentDate = GETDATE()SELECT CASE WHEN @EnterDate BETWEEN DATEADD(YEAR, -3, @CurrentDate) AND @CurrentDate THEN 'Valid' ELSE 'Not Valid' END
If you want to strip off the time portion of the DATETIME you can do soemthing like:
yes ur answer is very close but i want that the start date can be anything and end date also can be anything(not mandatory to be currentdate). now i want the start date not to be greater or less then end date not even one day greater or less.scoo