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



The information in this article applies to:

  • Microsoft SQL Server 2000 (all editions)

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

Modification Type:MajorLast Reviewed:11/6/2003
Keywords:kbBug kbfix kbSQLServ2000sp1fix KB290918