如何在 SQL 2000/2005 中执行区分大小写的搜索和替换?

发布于 2024-07-05 10:02:11 字数 122 浏览 8 评论 0原文

为了对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

小红帽 2024-07-12 10:02:11
SELECT testColumn FROM testTable  
    WHERE testColumn COLLATE Latin1_General_CS_AS = 'example' 

SELECT testColumn FROM testTable
    WHERE testColumn COLLATE Latin1_General_CS_AS = 'EXAMPLE' 

SELECT testColumn FROM testTable 
    WHERE testColumn COLLATE Latin1_General_CS_AS = 'eXaMpLe' 

不要假设默认排序规则将区分大小写,只需每次指定一种区分大小写的排序规则(当然,使用适合您的语言的正确排序规则)

SELECT testColumn FROM testTable  
    WHERE testColumn COLLATE Latin1_General_CS_AS = 'example' 

SELECT testColumn FROM testTable
    WHERE testColumn COLLATE Latin1_General_CS_AS = 'EXAMPLE' 

SELECT testColumn FROM testTable 
    WHERE testColumn COLLATE Latin1_General_CS_AS = 'eXaMpLe' 

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)

燃情 2024-07-12 10:02:11

如果您同一字段中的同一个单词有不同的大小写,并且只想替换特定的大小写,那么您可以在REPLACE 函数:

UPDATE tableName
SET fieldName = 
    REPLACE(
        REPLACE(
            fieldName COLLATE Latin1_General_CS_AS,
            'camelCase' COLLATE Latin1_General_CS_AS,
            'changedWord'
        ),
        'CamelCase' COLLATE Latin1_General_CS_AS,
        'ChangedWord'
    )

这将导致:

This is camelCase 1 and this is CamelCase 2

变成:

This is changedWord 1 and this is ChangedWord 2

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:

UPDATE tableName
SET fieldName = 
    REPLACE(
        REPLACE(
            fieldName COLLATE Latin1_General_CS_AS,
            'camelCase' COLLATE Latin1_General_CS_AS,
            'changedWord'
        ),
        'CamelCase' COLLATE Latin1_General_CS_AS,
        'ChangedWord'
    )

This will result in:

This is camelCase 1 and this is CamelCase 2

becoming:

This is changedWord 1 and this is ChangedWord 2
拔了角的鹿 2024-07-12 10:02:11

确定默认排序规则是否区分大小写,如下所示:

select charindex('RESULT', 'If the result is 0 you are in a case-sensitive collat​​ion mode')

结果为 0 表示您是在区分大小写的排序规则模式下,8表示不区分大小写。

如果排序规则不区分大小写,则需要在执行搜索/替换时显式声明要使用的排序规则模式。

以下是如何构建 UPDATE 语句,通过指定要使用的排序模式来执行区分大小写的搜索/替换:

update ContentTable
set ContentValue = replace(ContentValue COLLATE Latin1_General_BIN, 'THECONTENT', 'TheContent')
from StringResource
where charindex('THECONTENT', ContentValue COLLATE Latin1_General_BIN) > 0

这将匹配并替换 '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:

update ContentTable
set ContentValue = replace(ContentValue COLLATE Latin1_General_BIN, 'THECONTENT', 'TheContent')
from StringResource
where charindex('THECONTENT', ContentValue COLLATE Latin1_General_BIN) > 0

This will match and replace 'THECONTENT', but not 'TheContent' or 'thecontent'.

☆獨立☆ 2024-07-12 10:02:11

可以在多个语句中完成。
如果您的长字符串包含要替换的大写字母和小写字母,则此方法将不起作用
您可能还需要使用不同的排序规则,这是区分重音和大小写的。

UPDATE T SET [String] = ReplacedString
FROM [dbo].[TranslationText] T, 
    (SELECT [LanguageCode]
      ,[StringNo]
      ,REPLACE([String], 'Favourite','Favorite') ReplacedString
    FROM [dbo].[TranslationText]
    WHERE 
    [String] COLLATE Latin1_General_CS_AS like '%Favourite%'
    AND [LanguageCode] = 'en-us') US_STRINGS
WHERE 
T.[LanguageCode] = US_STRINGS.[LanguageCode] 
AND T.[StringNo] = US_STRINGS.[StringNo]

UPDATE T SET [String] = ReplacedString
FROM [dbo].[TranslationText] T, 
    (SELECT [LanguageCode]
      ,[StringNo]
      , REPLACE([String], 'favourite','favorite') ReplacedString 
    FROM [dbo].[TranslationText]
    WHERE 
    [String] COLLATE Latin1_General_CS_AS like '%favourite%'
    AND [LanguageCode] = 'en-us') US_STRINGS
WHERE 
T.[LanguageCode] = US_STRINGS.[LanguageCode] 
AND T.[StringNo] = US_STRINGS.[StringNo]

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.

UPDATE T SET [String] = ReplacedString
FROM [dbo].[TranslationText] T, 
    (SELECT [LanguageCode]
      ,[StringNo]
      ,REPLACE([String], 'Favourite','Favorite') ReplacedString
    FROM [dbo].[TranslationText]
    WHERE 
    [String] COLLATE Latin1_General_CS_AS like '%Favourite%'
    AND [LanguageCode] = 'en-us') US_STRINGS
WHERE 
T.[LanguageCode] = US_STRINGS.[LanguageCode] 
AND T.[StringNo] = US_STRINGS.[StringNo]

UPDATE T SET [String] = ReplacedString
FROM [dbo].[TranslationText] T, 
    (SELECT [LanguageCode]
      ,[StringNo]
      , REPLACE([String], 'favourite','favorite') ReplacedString 
    FROM [dbo].[TranslationText]
    WHERE 
    [String] COLLATE Latin1_General_CS_AS like '%favourite%'
    AND [LanguageCode] = 'en-us') US_STRINGS
WHERE 
T.[LanguageCode] = US_STRINGS.[LanguageCode] 
AND T.[StringNo] = US_STRINGS.[StringNo]
明月松间行 2024-07-12 10:02:11

首先检查一下:
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.

还给你自由 2024-07-12 10:02:11

另外,这可能有用。
select * from fn_helpcollat​​ions() - 这会获取服务器支持的所有排序规则。
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.

木有鱼丸 2024-07-12 10:02:11

您可以在每次查询表时指定排序规则,也可以通过更改表将排序规则永久应用于列。

如果您确实选择执行查询方法,那么包含不区分大小写的搜索参数也是有益的。 如果您包含它们,您将看到 SQL 将选择更有效的执行计划。 例如:

SELECT testColumn FROM testTable 
    WHERE testColumn COLLATE Latin1_General_CS_AS = 'eXaMpLe' 
    and testColumn = 'eXaMpLe'

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:

SELECT testColumn FROM testTable 
    WHERE testColumn COLLATE Latin1_General_CS_AS = 'eXaMpLe' 
    and testColumn = 'eXaMpLe'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文