Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

A Strange issue with the REPLACE Function Expand / Collapse
Author
Message
Posted Wednesday, April 08, 2009 8:31 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 2:12 AM
Points: 367, Visits: 849
Hi All,

I am using the replace function to remove all but alpha-numeric characters and have run into a little issue with superscript numbers.

The issue can be demonstrated (at least on my server) using the following code
DECLARE @str VARCHAR(100)
SET @str = '1234567890'
SELECT REPLACE(@str,CHAR(179),'_')

CHAR(179) is a superscript 3 and the result I get from the REPLACE is
12_4567890

The Server is SQL 2005 Standard Edition 64-bit, version 9.00.4035.00 with a collation of Latin1_General_CI_AS and language set to English(United States)

Any help or explanations would be gratefully received.

Llewy






RedLlewy
"The Plural of Anecdote is not Data"

Post #693176
Posted Wednesday, April 08, 2009 8:47 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, February 22, 2013 12:03 AM
Points: 2,555, Visits: 2,587
I don't know how the replace function behaves in different collations, but I know the problem is related with different collations.

Let' see it..

DECLARE @str VARCHAR(100)
SET @str = '1234567890'
SELECT REPLACE( @str, CHAR(179) COLLATE SQL_Latin1_General_CP1_CI_AS, '_' ) AS Using_SQL_Latin1_General_CP1_CI_AS,
REPLACE( @str, CHAR(179) COLLATE Latin1_General_CI_AS, '_' ) AS [Using_Latin1_General_CI_AS],
CHAR(179) AS Replacing_Character

Results:
Using_SQL_Latin1_General_CP1_CI_AS|Using_Latin1_General_CI_AS|Replacing_Character
1234567890|12_4567890|³


--Ramesh

Post #693201
Posted Wednesday, April 08, 2009 8:48 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:50 PM
Points: 21,624, Visits: 27,465
The issue is due to your using a case insensitive collation. Look at the following:

DECLARE @str VARCHAR(100)
SET @str = '1234567890'
SELECT REPLACE(@str collate Latin1_General_CI_AS,CHAR(179),'_')

SELECT REPLACE(@str collate Latin1_General_CS_AS,CHAR(179),'_')

SELECT REPLACE(@str collate Latin1_General_BIN2,CHAR(179),'_')




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #693204
Posted Wednesday, April 08, 2009 9:06 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 2:12 AM
Points: 367, Visits: 849
Thanks very much for that!

Does this imply that the REPLACE function treats CHAR(179) (superscript '3') as an Uppercase '3'?





RedLlewy
"The Plural of Anecdote is not Data"

Post #693225
Posted Wednesday, April 08, 2009 9:12 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:50 PM
Points: 21,624, Visits: 27,465
It isn't REPLACE that is doing this, it is your collation. In a case-insensitive collation 'a' = 'A'. As shown by your own example, in a case-insenstive collation there is no difference between a superscript 3 and a normal 3.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #693235
Posted Wednesday, April 08, 2009 10:00 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 2:12 AM
Points: 367, Visits: 849
Thank you very much for your help.


RedLlewy
"The Plural of Anecdote is not Data"

Post #693302
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse