FIX: Incorrect Behavior of LIKE [-] When Using an Index

This article was previously published under Q290918
This article has been archived. It is offered "as is" and will no longer be updated.
BUG #: 352098 (SHILOH_bugs)
SYMPTOMS
When using a wildcard search on a column containing the value [-], matching rows may be skipped if an index is used.
RESOLUTION
To resolve this problem, obtain the latest service pack for SQL Server 2000. For additional information, click the following article number to view the article in theMicrosoft Knowledge Base:
290211 INF: How to Obtain the Latest SQL Server 2000 Service Pack
WORKAROUND
The correct syntax should include the ESCAPE clause to search for characters that can indicate wildcard searches:
select * from dashes where no_key like 'a[~-]a%' ESCAPE '~'select * from dashes where primary_key like 'a[~-]a%' ESCAPE '~'				
STATUS
Microsoft has confirmed that this is a problem in SQL Server 2000. This problem was first corrected in SQL Server 2000 Service Pack 1.
MORE INFORMATION
-- Incorrect result set is produced by a "SELECT" which uses the "LIKE" -- clause where one argument is a primary key column and the other is a-- string literal with "[-]" in the middle of it.use northwindset nocount offif ( object_id('dashes') is not NULL ) drop table dashescreate table dashes ( primary_key varchar(20) primary key, no_key varchar(20) )insert dashes values ( 'a-a-key', 'a-a-value' )print 'next two statements correctly return one record each'select * from dashes where no_key like 'a-a%'select * from dashes where primary_key like 'a-a%'print 'for the primary key query, it incorrectly returns no rows'select * from dashes where no_key like 'a[-]a%'select * from dashes where primary_key like 'a[-]a%'				
next two statements correctly return one record eachprimary_key          no_key               -------------------- -------------------- a-a-key              a-a-value(1 row(s) affected)primary_key          no_key               -------------------- -------------------- a-a-key              a-a-value(1 row(s) affected)for the primary key query, it incorrectly returns no rowsprimary_key          no_key               -------------------- -------------------- a-a-key              a-a-value(1 row(s) affected)primary_key          no_key               -------------------- -------------------- (0 row(s) affected)				
incorrect wrong data result output wildcard hyphen dash
Properties

Article ID: 290918 - Last Review: 01/16/2015 21:50:18 - Revision: 3.2

  • Microsoft SQL Server 2000 Standard Edition
  • kbnosurvey kbarchive kbbug kbfix kbsqlserv2000sp1fix KB290918
Feedback