LIKE versus = for a text field / SQL Server

Results 1 to 2 of 2

Thread: LIKE versus = for a text field / SQL Server

  1. #1
    Join Date
    Dec 1969

    Default LIKE versus = for a text field / SQL Server

    I understand that "=" is more efficient than LIKE in a SQL Select statement if you know the exact value(s) of the condition such as Name = "bob" versus Name LIKE "bob". Correct?<BR><BR>If this is true then is there a "proper" way to code my SELECT statement to use "=" instead of LIKE even if the value of the condition is an exact value OR "%"?<BR><BR>I have a form with a series of drop downs that act as filters. Each drop down has "ANY" which is really has the value of "%" and then literal values. The form POSTS data to a "search results" page which takes the passed values and turns them into session variables. These session variables are used to filter the recordset.<BR><BR>The "problem" seems to be that it only works if I use LIKE because the value can be "%" instead of a literal value. So my question is (just looking for reassurance that I&#039;m right) that since I&#039;m dealing with text fields and the variable can be a literal value OR "%", I need to use LIKE instead of "=" -<BR>correct??<BR><BR>Example:<BR>SELECT * FROM dbo.vConnectionBudgetData_All WHERE ProjNum LIKE<BR>&#039;"&Session("sesProjNum")&"&#039; AND StatusID LIKE &#039;"&Session("sesStatus")&"&#039; AND etc.......<BR><BR><BR>

  2. #2
    Join Date
    Dec 1969

    Default RE: LIKE versus = for a text field / SQL Server

    You are correct. If you use a wildcard with = you&#039;ll get an error or incorrect results. Like doesn&#039;t process as fast, but you won&#039;t be able to tell the difference in speed between them.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts