Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CASE in WHERE + condition

Status
Not open for further replies.

RhythmAddict112

Programmer
Jun 17, 2004
625
US
Hey SQL Gurus,
Not sure if I'm just "not all there" today, or what...but here is the Q. I need to do a case statement in my where clause, this works:

Code:
WHERE r.[SomeCol] = @SomeColVal
AND SomeOtherCol =  
(
CASE WHEN (@Year = 0 AND @Period = 0) THEN
@SomeVal
CASE WHEN...
...
CASE ELSE
@SomeVal
END

But I need an additional OR clause in my else block, ex:
Code:
WHERE r.[SomeCol] = @SomeColVal
AND SomeOtherCol =  
(
CASE WHEN (@Year = 0 AND @Period = 0) THEN
@SomeVal
CASE WHEN...
...
CASE ELSE
@SomeVal OR @SomeVal - 1
END

The above throws the following error:
Incorrect syntax near the keyword 'OR'. within the ELSE statement - not a surprise. I'm sure there is a workaround for this, but I just can't think of it right now. Any input would be great...thanks!


 
WHERE r.[SomeCol] = @SomeColVal
AND (@SomeVal = -1 OR SomeOtherCol =
(
CASE WHEN (@Year = 0 AND @Period = 0) THEN
@SomeVal
CASE WHEN...
...
CASE ELSE
@SomeVal
END)
 
Code:
WHERE r.[SomeCol] = @SomeColVal AND
      r.SomeOtherCol = CASE WHEN (@Year = 0 AND @Period = 0)
                                  THEN @SomeVal
                            WHEN...
                                  THEN ...

                            WHEN r.SomeOtherCol = @SomeVal - 1
                                 THEN @SomeVal - 1

                       ELSE @SomeVal 
                       END

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top