如何使用html实体搜索记录集

时间:2011-03-11 10:52:57

标签: mysql sql html-entities

我数据库中的大多数数据都存储有html实体,例如ä而不是ä。但有些数据存储为纯文本(ä)。

现在我想找到ä或ä的所有记录集。如何在不使用

的情况下达到最佳效果

SELECT id FROM table WHERE content LIKE'%ä%'或内容LIKE'%ä%'

2 个答案:

答案 0 :(得分:1)

您可以在替换要在同一查询中搜索的值时搜索表格:

SELECT * FROM table WHERE REPLACE(content,'ä','ä') LIKE '%ä%'

当然,你必须为所有的Umlaute做替换。

答案 1 :(得分:0)

最佳答案是浏览现有数据并将其中所有实例更改为一致。

创建UDF

Create Function dbo.ReplaceHtmlEntities(@arg NVARCHAR(MAX) collate Latin1_General_Bin)
returns NVARCHAR(MAX) 
as
begin
   if @arg is null return @arg
   if not @arg like '%&%;%' return @arg
   -- Collation matters here obviously!!
   -- Auto generated lines
   -- These lines should be generated from a list of entities and Unicode values
   -- In practice you can limit this to the ones you actually have a problem with 
   set @arg = replace(@arg, 'Ä' collate Latin1_General_BIN, char(0xUUUU))
   set @arg = replace(@arg, 'ä' collate Latin1_General_BIN, char(0xUUUU))
   set @arg = replace(@arg, 'Ö' collate Latin1_General_BIN, char(0xUUUU))
   set @arg = replace(@arg, 'ö' collate Latin1_General_BIN, char(0xUUUU))
   -- For speed you can group them more common first, and short-circuit where possible
   if not @arg like '%&%;%' return @arg

   -- a lot more lines.... 
   return @arg
end

然后你只需要经过你所有的桌子!!!!