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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie: If Then within where clause

Status
Not open for further replies.

lcoppenr

IS-IT--Management
Nov 5, 2004
1
US
I'm trying to write the following stored procedure, but SQL has issues with the If statement. How do I include an case/if/etc into the where clause?

CREATE procedure [dbo].[MyProcedure]
@blnCheckbox bit = 1
As

Select Field1,
Field2,
Field3
From MyTable
Where Field1 like '%blah%'
and Field2 like '%blahblah%'
If @blnCheckbox = 0
Begin
and Field3 like '%blah%'
End

 

Maybe this would work:
Code:
CREATE procedure [dbo].[MyProcedure]
    @blnCheckbox bit = 1
As

Select Field1,
     Field2,
     Field3
From MyTable
Where Field1 like '%blah%'
     and Field2 like '%blahblah%'
     and ((@blnCheckbox = 0 and Field3 like '%blah%')
        or @blnCheckbox != 0 )
[3eyes]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Why not simply this ,
WHERE Field1 LIKE '%blah%'
AND Field2 LIKE '%blahblah%'
AND (@blnCheckbox != 0 OR Field3 LIKE '%blah%')


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top