condition in where clause with optional parameter

Paran0id

New Member
There are thee input parameter in my stored procedure. for example, \[code\]DECLARE @fromDate DateTime = NULL DECLARE @toDate DateTime = NULLDECLARE @Id int = NULL\[/code\]I want to write a condition in where clause like...if fromDate is provided then searching must be done on @fromDate. if @fromDate is not provided then check for @Id variable if this is not null then search basis on @Id...Something like...\[code\] where CASE when @fromDate is not null THEN (@FromDate is null or ([Created] between @FromDate and @ToDate)) ELSE (@requestId is null or Id=@requestId)\[/code\]there is one problem with below solution...if @fromDate and @Id both are provided then this will do intesect of them and nothing is return.....condition should be like...if @fromDate is given the priority gives to @fromDate even if @Id is provided and result must not be dependend to @Id parameter....
 
Top