2

Why do i get only one Result?

    select replace(N'2',N'2','X') as Text
    union 
    select replace(N'2',N'²','X') as Text

The second SELECT statement includes a squared.

@@version= Microsoft SQL Server 2012 - 11.0.5623.0

5
  • N'2' must be getting treated identically as N'²'. Commented Aug 27, 2016 at 10:40
  • Are you certain that you really entered a squared into the query? Commented Aug 27, 2016 at 10:47
  • Here is the proof select 1 where N'²'=N'2' returns 1. Looks like in SQL SERVER '²'='2'. So both the selects replaced with X Commented Aug 27, 2016 at 10:56
  • 1
    However the ASCII value differs. SELECT ASCII('²'),ASCII('2') returns 178 and 50 respectively Commented Aug 27, 2016 at 11:00
  • Replace as a function is limited in part to your environment. For example, ASCII codes 32 and 255 (space and non-breaking space) are treated exactly the same unless specifically called. Your environment shows a space, but they are not. In the same way,you may find using the actual ASCII code in your function ensures accuracy. Basically, are you sure REPLACE is actually reading it like you think it is? Commented Aug 27, 2016 at 12:57

1 Answer 1

3

The behavior depends on the case-sensitivity of the collation:

SELECT REPLACE(N'2' COLLATE Latin1_General_CS_AS, N'2'  COLLATE Latin1_General_CS_AS,'X'  COLLATE Latin1_General_CS_AS) as Text
UNION 
SELECT REPLACE(N'2' COLLATE Latin1_General_CS_AS, N'²'  COLLATE Latin1_General_CS_AS,'X'  COLLATE Latin1_General_CS_AS) as Text
GO
--results:
X
2
SELECT REPLACE(N'2' COLLATE Latin1_General_CI_AS, N'2'  COLLATE Latin1_General_CI_AS,'X'  COLLATE Latin1_General_CI_AS) as Text
UNION 
SELECT REPLACE(N'2' COLLATE Latin1_General_CI_AS, N'²'  COLLATE Latin1_General_CI_AS,'X'  COLLATE Latin1_General_CI_AS) as Text
GO
--results:
X

When no collation is specified for literal expressions, the database default collation is used.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.