如何在 SQL 2000/2005 中执行区分大小写的搜索和替换?
为了对 SQL Server 2000/2005 数据库中的表执行区分大小写的搜索/替换,必须使用正确的排序规则。
如何确定数据库的默认排序规则是否区分大小写,如果不区分大小写,如何执行区分大小写的搜索/替换?
In order to perform a case-sensitive search/replace on a table in a SQL Server 2000/2005 database, you must use the correct collation.
How do you determine whether the default collation for a database is case-sensitive, and if it isn't, how to perform a case-sensitive search/replace?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不要假设默认排序规则将区分大小写,只需每次指定一种区分大小写的排序规则(当然,使用适合您的语言的正确排序规则)
Don't assume the default collation will be case sensitive, just specify a case sensitive one every time (using the correct one for your language of course)
如果您同一字段中的同一个单词有不同的大小写,并且只想替换特定的大小写,那么您可以在
REPLACE
函数:这将导致:
变成:
If you have different cases of the same word in the same field, and only want to replace specific cases, then you can use collation in your
REPLACE
function:This will result in:
becoming:
确定默认排序规则是否区分大小写,如下所示:
select charindex('RESULT', 'If the result is 0 you are in a case-sensitive collation mode')
结果为 0 表示您是在区分大小写的排序规则模式下,8表示不区分大小写。
如果排序规则不区分大小写,则需要在执行搜索/替换时显式声明要使用的排序规则模式。
以下是如何构建 UPDATE 语句,通过指定要使用的排序模式来执行区分大小写的搜索/替换:
这将匹配并替换
'THECONTENT'
,但不会匹配和替换'TheContent' 或<代码>'内容'。
Determine whether the default collation is case-sensitive like this:
select charindex('RESULT', 'If the result is 0 you are in a case-sensitive collation mode')
A result of 0 indicates you are in a case-sensitive collation mode, 8 indicates it is case-insensitive.
If the collation is case-insensitive, you need to explicitly declare the collation mode you want to use when performing a search/replace.
Here's how to construct an UPDATE statement to perform a case-sensitive search/replace by specifying the collation mode to use:
This will match and replace
'THECONTENT'
, but not'TheContent'
or'thecontent'
.可以在多个语句中完成。
如果您的长字符串包含要替换的大写字母和小写字母,则此方法将不起作用。
您可能还需要使用不同的排序规则,这是区分重音和大小写的。
Can be done in multiple statements.
This will not work if you have long strings that contain both capitalized an lowercase words you intend to replace.
You might also need to use different collation this is accent and case sensitive.
首先检查一下:
http://technet.microsoft.com/en-us /library/ms180175(SQL.90).aspx
您将看到 CI 指定不区分大小写,CS 指定区分大小写。
First of all check this:
http://technet.microsoft.com/en-us/library/ms180175(SQL.90).aspx
You will see that CI specifies case-insensitive and CS specifies case-sensitive.
另外,这可能有用。
select * from fn_helpcollations() - 这会获取服务器支持的所有排序规则。
select * from sys.databases - 这里有一列指定服务器上每个数据库的排序规则。
Also, this might be usefull.
select * from fn_helpcollations() - this gets all the collations your server supports.
select * from sys.databases - here there is a column that specifies what collation has every database on your server.
您可以在每次查询表时指定排序规则,也可以通过更改表将排序规则永久应用于列。
如果您确实选择执行查询方法,那么包含不区分大小写的搜索参数也是有益的。 如果您包含它们,您将看到 SQL 将选择更有效的执行计划。 例如:
You can either specify the collation every time you query the table or you can apply the collation to the column(s) permanently by altering the table.
If you do choose to do the query method its beneficial to include the case insensitive search arguments as well. You will see that SQL will choose a more efficient exec plan if you include them. For example: