替换仅匹配字符串的开头 [英] replace only matches the beginning of the string

查看:200
本文介绍了替换仅匹配字符串的开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个函数,以将罗马尼亚音标字母( ĂÎȘȚ )替换为拉丁字母等效项( AAIST

I'm trying to write a function to replace the Romanian diacritic letters (ĂÂÎȘȚ) to their Latin letter equivalents (AAIST, respectively).

SQL Server的 replace 函数处理Ă Î很好。

SQL Server's replace function deals with Ă, Â, and Î just fine.

Ș Ț 似乎有一个奇怪的问题:但是,只有在它们

It seems to have a weird problem with Ș and Ț, though: they are only replaced if they are found at the beginning of the string.

对于示例

select replace(N'Ș', N'Ș', N'-')
-- '-'   # OK

select replace(N'ȘA', N'Ș', N'-')
-- '-A'  # OK

select replace(N'AȘ', N'Ș', N'-')
-- 'AȘ'  # WHAT??

select replace(N'ȘAȘ', N'Ș', N'-')
-- '-AȘ' # WHAT??

我设法在SQL Sever 2008 R2和SQL Server 2012上重现此行为。

I managed to reproduce this behavior on both SQL Sever 2008 R2 and SQL Server 2012.

这些看似奇怪的结果是否有解释?还是仅仅是一个错误?

Is there an explanation for these seemingly weird results? Or could it be just a bug?

我的默认数据库排序规则是 SQL_Latin1_General_CP1_CI_AS

My default database collation is SQL_Latin1_General_CP1_CI_AS.

推荐答案

这是排序规则问题。

乍看之下,我不得不重现,因为它难以置信,但是您的查询对我来说也是同样的问题。

It is a collation problem.
At first look, I had to reproduce, because it was unbelivable, but your query had the same problem for me.

如果您尝试使用正确的排序规则可以正常工作:

If you try with a proper collation it works:

select replace(N'AȘ' COLLATE Latin1_General_BIN, N'Ș', N'-')

这篇关于替换仅匹配字符串的开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆