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
 Transact-SQL (2000)
 IF or CASE statement in WHERE clause

Author  Topic 

Rawager
Starting Member

2 Posts

Posted - 2003-01-24 : 12:14:39
I need to pull records based on Dates.

Example.
IF Today is between 1 and 15 then pull records dated within a specific date range Else records dated within another date range.

This condition needs to be validated along with other conditions in the WHERE clause.

Any thoughts?

......R

ValterBorges
Master Smack Fu Yak Hacker

1429 Posts

Posted - 2003-01-24 : 12:37:07
WHERE
(Day(getdate()) < = 15 AND mydate between startdate and enddate) OR
(Day(getdate()) > 15 AND mydate between startdate and enddate


Go to Top of Page

Juls
Yak Posting Veteran

51 Posts

Posted - 2003-01-24 : 12:40:19
You can build a dynamic WHERE clause and do if else statement and attaching correct dates to WHERE.

DECLARE @WHERe varchar(4000)
SET @WHERE = 'WHERE '
If datepart(dd,getdate())>=1 and datepart(dd,getdate())<=15
SET @WHERE = @WHERE + 'mydate between Date1 and Date2'

Something like above

Go to Top of Page

Rawager
Starting Member

2 Posts

Posted - 2003-01-24 : 14:54:51
Thank you guys. That gave me few more ideas to try out.

......R

Go to Top of Page
   

- Advertisement -