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

Article translations Article translations
Article ID: 290918 - View products that this article applies to.
This article was previously published under Q290918
BUG #: 352098 (SHILOH_bugs)
Expand all | Collapse all

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 "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 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)
				

Properties

Article ID: 290918 - Last Review: November 6, 2003 - Revision: 3.2
APPLIES TO
  • Microsoft SQL Server 2000 Standard Edition
Keywords: 
kbbug kbfix kbsqlserv2000sp1fix KB290918

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com