Replace Unicode characters in T-SQL

后端 未结 2 1323
再見小時候
再見小時候 2020-12-20 21:46

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

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

The result I\'m gettin

相关标签:
2条回答
  • 2020-12-20 22:06

    This might work for you:

    DECLARE @text NVARCHAR(1000) = N'this is the news with a þ';
    DECLARE @find NVARCHAR(1000) = N'þ';
    DECLARE @replace NVARCHAR(1000) = N'_';
    
    SELECT REPLACE(CAST(@text AS VARCHAR), CAST(@find AS VARCHAR), CAST(@replace AS VARCHAR));
    
    0 讨论(0)
  • 2020-12-20 22:17

    The þ 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 'þ' (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, 'þ' and 'Þ' should be treated as just a tertiary-level difference from the letters "th" and "TH" respectively.

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

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

    Returns:

    this is the news with a __
    

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

    0 讨论(0)
提交回复
热议问题