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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 date functions

Author  Topic 

PeterG
Posting Yak Master

156 Posts

Posted - 2003-10-20 : 12:35:54
How do i code this?

Is today's date less than a certain date + 5 days?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-10-20 : 12:38:03
DECLARE @var1 DATETIME

SET @var1 = 'Jan 12 2003'

IF GETDATE() < @var1 + 5 -- when you add a number to a date column, it adds it to the day
PRINT 'Do Something'
ELSE
PRINT 'Do Something Else'

Tara
Go to Top of Page

PeterG
Posting Yak Master

156 Posts

Posted - 2003-10-20 : 12:42:45
Thanks Tara.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-10-20 : 12:49:12
My 2 cents...


DECLARE @var1 datetime
SET @var1 = 'Jan 12 2003'
IF (SELECT DATEDIFF(d,GETDATE(),(@var1 + 5)))< 0
PRINT 'Do Something'
ELSE
PRINT 'Do Something Else'




Brett

8-)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-10-20 : 12:51:44
Yep, Brett's is better. His will allow the use of an index if @var1 is a column that is indexed.

Tara
Go to Top of Page

PeterG
Posting Yak Master

156 Posts

Posted - 2003-10-20 : 13:56:19
what about if today's date is the same as a certain date + 5? Will this syntax also work?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-10-20 : 13:58:32
Just put <= instead of <.

Why not test it out using your data?

Tara
Go to Top of Page

PeterG
Posting Yak Master

156 Posts

Posted - 2003-10-20 : 14:09:56
actually it should be >= instead of <= as getdate() is the start date and @var1 + 5 is the end date.

Acctg to BOL, "If startdate is later than enddate, a negative value is returned."

In my case I want to find out if @getdate(), i.e., today's date is less (or earlier) than @var1 + 5.

Correct?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-10-20 : 14:15:45
Why not test it out? I'll get you started:

DECLARE @var1 DATETIME
DECLARE @var2 DATETIME

SET @var1 = 'Jan 13 2003'
SET @var2 = GETDATE()

IF (SELECT DATEDIFF(d, @var2, (@var1 + 5))) >= 0
PRINT 'Do Something'
ELSE
PRINT 'Do Something Else'

Tara
Go to Top of Page
   

- Advertisement -