Microsoft KB Archive/290918

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

Article ID: 290918

Article Last Modified on 11/6/2003

-

APPLIES TO


 * Microsoft SQL Server 2000 Standard Edition

-



This article was previously published under Q290918



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 the Microsoft 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 &quot;SELECT&quot; which uses the &quot;LIKE&quot; -- clause where one argument is a primary key column and the other is a -- string literal with &quot;[-]&quot; in the middle of it. use northwind set nocount off

if ( object_id('dashes') is not NULL ) drop table dashes create 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 each primary_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 rows primary_key         no_key a-a-key             a-a-value

(1 row(s) affected)

primary_key         no_key

(0 row(s) affected)

Additional query words: incorrect wrong data result output wildcard hyphen dash

Keywords: kbbug kbfix kbsqlserv2000sp1fix KB290918

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.