Doing away with like '%%'

Here's the setup

@searchcondition varchar(50) --is a parameter passed into a stored procedure

IF(@searchconditions is null)

BEGIN

SET @searchcondition = '%'

END

ELSE

BEGIN

SET @searchcondtion = '%' + @searchcondition + '%'

END

select * from sometable

where somecolumn like @searchcondition

Now the procedure is fine if the user passes a string, but if it's null, it seems really inefficient to search for '%'. Is there a generalized approach, other than say putting slightly different versions of the query in an if block



Answer this question

Doing away with like '%%'

  • socko

    I'm trying to make minor optimizations to a setup I do not have full control of. Thanks for the pointer to the fulltext stuff though, I'll definitely be using that on my future projects.
  • Sharad_Sharma_2k

    Nice, works like a charm. Thanks for the tip.
  • vb_jonas

    Have you considering using the Fulltext features in SQL Server 2005 rather than LIKE
  • Carolus.Holman

    Did you already try the following:

    select * from sometable

    where (somecolumn like @searchcondition) or (@searchcondition is null)


  • Doing away with like '%%'