Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

sql server - Replace Unicode characters in T-SQL

How do I replace only the last character of the string:

select REPLACE('this is the news with a t', 't', '__')

The result I'm getting is:

__is is __e news wi__ a __

EDIT The collation of the server and the database is Latin1_General_CI_AS

The actual query I'm running is REPLACE(note, 't', '') where note is an ntext column. The point is to strip out the thorn characters because that character gets used later in the process as a column delimiter. (Please don't suggest changing the delimiter, that's just not going to happen given the extent to which it's been used!)

I've tried using the N prefix even using the test select statement, here are the results:

Still broken!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The t character (Extended ASCII { via ISO-8859-1 and ANSI Code Page 1252 } & UNICODE value of 254) is known as "thorn" and in some languages equates directly to th:

  • Technical info on the character here: http://unicode-table.com/en/00FE/

  • Explanation of that character and collations here: http://userguide.icu-project.org/collation/customization. Search the page — typically Control-F — for "Complex Tailoring Examples" and you will see the following:

    The letter 't' (THORN) is normally treated by UCA/root collation as a separate letter that has primary-level sorting after 'z'. However, in Swedish and some other Scandinavian languages, 't' and 'T' should be treated as just a tertiary-level difference from the letters "th" and "TH" respectively.

If you do not want t to equate to th, then force a Binary collation as follows:

SELECT REPLACE(N'this is the news with a t' COLLATE Latin1_General_100_BIN2,
                 N't', N'__');

Returns:

this is the news with a __

For more info on working with Collations, Unicode, encodings, etc, please visit: Collations Info


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...