dbTalk Databases Forums  

Unexpected LIKE behavior

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Unexpected LIKE behavior in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Dimitri Furman
 
Posts: n/a

Default Unexpected LIKE behavior - 04-21-2007 , 09:18 PM






SQL Server 2000 SP4.

Running the script below prints 'Unexpected':

-----------------------------
DECLARE @String AS varchar(1)

SELECT @String = 'z'

IF @String LIKE '[' + CHAR(32) + '-' + CHAR(255) + ']'
PRINT 'Expected'
ELSE
PRINT 'Unexpected'
-----------------------------

If the @String variable is set to 'y' (or in fact any ANSI character other
than 'z'), the result is 'Expected'. The comparison also evaluates as
expected if CHAR(255) is replaced with CHAR(254). The server collation, if
that matters, is SQL_Latin1_General_CP1_CI_AS.

It would be helpful to find the explanatin of this behavior. Thanks.

--
(remove a 9 to reply by email)

Reply With Quote
  #2  
Old   
Ed Murphy
 
Posts: n/a

Default Re: Unexpected LIKE behavior - 04-22-2007 , 01:01 AM






Dimitri Furman wrote:

Quote:
SQL Server 2000 SP4.

Running the script below prints 'Unexpected':

-----------------------------
DECLARE @String AS varchar(1)

SELECT @String = 'z'

IF @String LIKE '[' + CHAR(32) + '-' + CHAR(255) + ']'
PRINT 'Expected'
ELSE
PRINT 'Unexpected'
-----------------------------

If the @String variable is set to 'y' (or in fact any ANSI character other
than 'z'), the result is 'Expected'. The comparison also evaluates as
expected if CHAR(255) is replaced with CHAR(254). The server collation, if
that matters, is SQL_Latin1_General_CP1_CI_AS.

It would be helpful to find the explanatin of this behavior. Thanks.
CHAR(255) = 'y' with an umlaut
http://englishplus.com/xascii.htm


Reply With Quote
  #3  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Unexpected LIKE behavior - 04-22-2007 , 04:37 AM



Dimitri Furman (dfurman (AT) cloud99 (DOT) net) writes:
Quote:
SQL Server 2000 SP4.

Running the script below prints 'Unexpected':

-----------------------------
DECLARE @String AS varchar(1)

SELECT @String = 'z'

IF @String LIKE '[' + CHAR(32) + '-' + CHAR(255) + ']'
PRINT 'Expected'
ELSE
PRINT 'Unexpected'
-----------------------------

If the @String variable is set to 'y' (or in fact any ANSI character other
than 'z'), the result is 'Expected'. The comparison also evaluates as
expected if CHAR(255) is replaced with CHAR(254). The server collation, if
that matters, is SQL_Latin1_General_CP1_CI_AS.

It would be helpful to find the explanatin of this behavior. Thanks.
I suspect that this is a case of wrong expectations. When you have a
range in a pattern, it is not defined from ASCII codes, but from the
collation. Only if the collation is a binary collation, character codes
apply. As Ed said char(255) is ÿ, so z is definitely outside the range.

This prints "Expected":

DECLARE @String AS varchar(1)

SELECT @String = 'z'

IF @String COLLATE Latin1_General_BIN
LIKE '[' + CHAR(32) + '-' + CHAR(255) + ']'
PRINT 'Expected'
ELSE
PRINT 'Unexpected'


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.