0

can a variable be turned on or off?

from the below code what if i want to search for all clients and not just dog ppl is there a easy way to search all or do i have to comment the client=@client out from the where statement?

declare @start as date
declare @end as date
declare @client as varchar(50)
declare @Code as varchar(100)
set @start = '2017-05-28'
set @end = '2017-05-30'
set @client = 'dog ppl'
set @Code = 'in,out'
;with cte as (

select  sql code

where 
entered between @start and @end -- order date created
--finished between @start and @end --order closed
and 
client=@client
and Code in(select List from [RAT2].dbo.[tfn_SplitList](@Code,','))
RJ_
  • 3
  • 2
  • check this post out. https://stackoverflow.com/questions/3415582/how-can-i-use-optional-parameters-in-a-t-sql-stored-procedure – Jeremy Jun 09 '17 at 17:48

1 Answers1

3

Use

(client = @client or @client is null)

Then if your variable is NULL it will not be considered in the result set.

juergen d
  • 201,996
  • 37
  • 293
  • 362