View Single Post

   
  #3 (permalink)  
Old 02-29-2008, 01:56 PM
ZeldorBlat
 
Posts: n/a
Default Re: Creating WHERE clauses based on IF (or CASE) STATEMENTS Transact-SQL

>How about this:
>
>if @myparam = '' or @myparam is null
> @myparam = '%'
>else
> @myparam = '%' + @myparam + '%'
>
>select ...
>from mytable
>where MyField like @myparam


Actually, now that I've thought about it more, it would be silly (and
wasteful) to pattern match to everything. This might be better:

select ...
from mytable
where (@myparam = '' or @myparam is null
or charindex(@myparam,[MyField]) > 0))

Reply With Quote