Re: If Statement to determine WHERE condition in SQL Query
- From: "Anith Sen" <anith@xxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 12 May 2005 10:55:47 -0500
In general, you'd use a CASE statement for such requirements, for instance:
SELECT Forename, Username, Description
FROM Audit
WHERE Date >= @Param_StartDate
AND Date < @Param_EndDate
AND UserID = CASE WHEN @Param_UserKey <> -1
THEN @Param_UserKey
ELSE UserID
END ;
If the column is nullable, you will have to use COALESCE or perhaps re-write
it as:
...AND ( @Param_UserKey <> -1 AND UserID = @Param_UserKey )
OR ( @Param_UserKey <> -1 )
And there are several other approaches for such requirements, some of which
are detailed at:
http://www.sommarskog.se/dyn-search.html
--
Anith
.
- References:
- If Statement to determine WHERE condition in SQL Query
- From: Rhonda Fischer
- If Statement to determine WHERE condition in SQL Query
- Prev by Date: If Statement to determine WHERE condition in SQL Query
- Next by Date: Re: If Statement to determine WHERE condition in SQL Query
- Previous by thread: If Statement to determine WHERE condition in SQL Query
- Next by thread: Re: If Statement to determine WHERE condition in SQL Query
- Index(es):
Relevant Pages
|
|