我正在尝试编写一个UDF(实际上我正在将我在网络上找到的一些代码改编成一个函数)来执行标题中描述的内容。
以下是代码:
declare @txt varchar(max), @start int, @end int, @len int
set @txt = '<p class="answer">Informamos que a documentação <strong>deve ser impressa e enviada fisicamente pela AGÊNCIA</strong>, contendo confere com oringinal por funcionário CAIXA.</p>'
set @start = charindex('<',@txt)
set @end = charindex('>',@txt,@start)
set @len = (@end - @start) + 1
while @start > 0 and @end > 0 and @len > 0
begin
set @txt = stuff(@txt,@start,@len,'')
set @start = charindex('<',@txt)
set @end = charindex('>',@txt,@start)
set @len = (@end - @start) + 1
end
SET @txt = REPLACE(@txt,' ',' ') --space
SET @txt = REPLACE(@txt,'“',CHAR(34)) --"
SET @txt = REPLACE(@txt,'”',CHAR(34)) --"
SET @txt = REPLACE(@txt,'‘',CHAR(39)) --'
SET @txt = REPLACE(@txt,'’',CHAR(39)) --'
SET @txt = REPLACE(@txt,'–',CHAR(150)) -- –
SET @txt = REPLACE(@txt,'—',CHAR(151)) -- —
SET @txt = REPLACE(@txt,'º',CHAR(186)) -- º
SET @txt = REPLACE(@txt,'ª',CHAR(170)) -- ª
SET @txt = REPLACE(@txt,'§',CHAR(167)) -- §
--------------------------------------------------------------
SET @txt = REPLACE(@txt,'"',CHAR(34)) --"
SET @txt = REPLACE(@txt,''',CHAR(39)) --'
--------------------------------------------------------------
SET @txt = REPLACE(@txt,'à','à') --à
SET @txt = REPLACE(@txt,'á','á') --á
SET @txt = REPLACE(@txt,'ã','ã') --ã
SET @txt = REPLACE(@txt,'â','â') --â
SET @txt = REPLACE(@txt,'ä','ä') --ä
SET @txt = REPLACE(@txt,'é','é') --é
SET @txt = REPLACE(@txt,'ê','ê') --ê
SET @txt = REPLACE(@txt,'í','í') --í
SET @txt = REPLACE(@txt,'ó','ó') --ó
SET @txt = REPLACE(@txt,'õ','õ') --õ
SET @txt = REPLACE(@txt,'ø','ø') --ø
SET @txt = REPLACE(@txt,'ú','ú') --ú
SET @txt = REPLACE(@txt,'ü','ü') --ü
SET @txt = REPLACE(@txt,'ç','ç') --ç
--------------------------------------------------------------
SET @txt = REPLACE(@txt,'À',CHAR(192)) --À
SET @txt = REPLACE(@txt,'Á',CHAR(193)) --Á
SET @txt = REPLACE(@txt,'Ã',CHAR(195)) --Ã
SET @txt = REPLACE(@txt,'Â',CHAR(194)) --Â
SET @txt = REPLACE(@txt,'Ä',CHAR(196)) --Ä
SET @txt = REPLACE(@txt,'É',CHAR(201)) --É
SET @txt = REPLACE(@txt,'Ê',CHAR(202)) --Ê
SET @txt = REPLACE(@txt,'Í',CHAR(205)) --Í
SET @txt = REPLACE(@txt,'Ó',CHAR(211)) --Ó
SET @txt = REPLACE(@txt,'Õ',CHAR(213)) --Õ
SET @txt = REPLACE(@txt,'Ø',CHAR(216)) --Ø
SET @txt = REPLACE(@txt,'Ú',CHAR(218)) --Ú
SET @txt = REPLACE(@txt,'Ü',CHAR(220)) --Ü
SET @txt = REPLACE(@txt,'Ç',CHAR(199)) --Ç
select LTRIM(RTRIM(@txt))
它正在剥离HTML标记,只转换为小写HTML实体,在单词AG Ê
NCIA(AGÊNCIA)中找到大写如Ê
时不起作用,而不是打印AGêNCIA。
有任何帮助使其正常工作吗?
编辑:PS:我无法更改我的数据库排序规则,正如@dzomba所建议的
答案 0 :(得分:2)
如果您需要,则需要将db collation设置为区分大小写。
UPDATE!这是一种解决方法,但我认为这将完成这项工作。我没有一个SQL服务器来测试它,但我几乎可以肯定它的工作正常。
declare @txt varchar(max)
declare @start int
declare @end int
declare @len int
set @txt = '<p class="answer">Informamos que a documentação <strong>deve ser impressa e enviada fisicamente pela AGÊNCIA</strong>, contendo confere com oringinal por funcionário CAIXA.</p>'
set @start = charindex('<',@txt)
set @end = charindex('>',@txt,@start)
set @len = (@end - @start) + 1
while @start > 0 and @end > 0 and @len > 0
begin
set @txt = stuff(@txt,@start,@len,'')
set @start = charindex('<',@txt)
set @end = charindex('>',@txt,@start)
set @len = (@end - @start) + 1
end
DECLARE @table (txtColumn varchar(max) COLLATE SQL_Latin1_General_CP1_CS_AS ) --make the column case sensitive
INSERT INTO @table (txtColumn)
SELECT @txt
UPDATE @table set txtColumn = REPLACE(txtColumn,' ',' ') --space
UPDATE @table set txtColumn = REPLACE(txtColumn,'“',CHAR(34)) --"
UPDATE @table set txtColumn = REPLACE(txtColumn,'”',CHAR(34)) --"
UPDATE @table set txtColumn = REPLACE(txtColumn,'‘',CHAR(39)) --'
UPDATE @table set txtColumn = REPLACE(txtColumn,'’',CHAR(39)) --'
UPDATE @table set txtColumn = REPLACE(txtColumn,'–',CHAR(150)) -- –
UPDATE @table set txtColumn = REPLACE(txtColumn,'—',CHAR(151)) -- —
UPDATE @table set txtColumn = REPLACE(txtColumn,'º',CHAR(186)) -- º
UPDATE @table set txtColumn = REPLACE(txtColumn,'ª',CHAR(170)) -- ª
UPDATE @table set txtColumn = REPLACE(txtColumn,'§',CHAR(167)) -- §
-------------------------------------------------------------
UPDATE @table set txtColumn = REPLACE(txtColumn,'"',CHAR(34)) --"
UPDATE @table set txtColumn = REPLACE(txtColumn,''',CHAR(39)) --'
--------------------------------------------------------------
UPDATE @table set txtColumn = REPLACE(txtColumn,'à','à') --à
UPDATE @table set txtColumn = REPLACE(txtColumn,'á','á') --á
UPDATE @table set txtColumn = REPLACE(txtColumn,'ã','ã') --ã
UPDATE @table set txtColumn = REPLACE(txtColumn,'â','â') --â
UPDATE @table set txtColumn = REPLACE(txtColumn,'ä','ä') --ä
UPDATE @table set txtColumn = REPLACE(txtColumn,'é','é') --é
UPDATE @table set txtColumn = REPLACE(txtColumn,'ê','ê') --ê
UPDATE @table set txtColumn = REPLACE(txtColumn,'í','í') --í
UPDATE @table set txtColumn = REPLACE(txtColumn,'ó','ó') --ó
UPDATE @table set txtColumn = REPLACE(txtColumn,'õ','õ') --õ
UPDATE @table set txtColumn = REPLACE(txtColumn,'ø','ø') --ø
UPDATE @table set txtColumn = REPLACE(txtColumn,'ú','ú') --ú
UPDATE @table set txtColumn = REPLACE(txtColumn,'ü','ü') --ü
UPDATE @table set txtColumn = REPLACE(txtColumn,'ç','ç') --ç
-------------------------------------------------------------
UPDATE @table set txtColumn = REPLACE(txtColumn,'À',CHAR(192)) --À
UPDATE @table set txtColumn = REPLACE(txtColumn,'Á',CHAR(193)) --Á
UPDATE @table set txtColumn = REPLACE(txtColumn,'Ã',CHAR(195)) --Ã
UPDATE @table set txtColumn = REPLACE(txtColumn,'Â',CHAR(194)) --Â
UPDATE @table set txtColumn = REPLACE(txtColumn,'Ä',CHAR(196)) --Ä
UPDATE @table set txtColumn = REPLACE(txtColumn,'É',CHAR(201)) --É
UPDATE @table set txtColumn = REPLACE(txtColumn,'Ê',CHAR(202)) --Ê
UPDATE @table set txtColumn = REPLACE(txtColumn,'Í',CHAR(205)) --Í
UPDATE @table set txtColumn = REPLACE(txtColumn,'Ó',CHAR(211)) --Ó
UPDATE @table set txtColumn = REPLACE(txtColumn,'Õ',CHAR(213)) --Õ
UPDATE @table set txtColumn = REPLACE(txtColumn,'Ø',CHAR(216)) --Ø
UPDATE @table set txtColumn = REPLACE(txtColumn,'Ú',CHAR(218)) --Ú
UPDATE @table set txtColumn = REPLACE(txtColumn,'Ü',CHAR(220)) --Ü
UPDATE @table set txtColumn = REPLACE(txtColumn,'Ç',CHAR(199)) --Ç
SELECT LTRIM(RTRIM(txtColumn)) FROM @table
--- IN THE END DROP THE TABLE
DROP TABLE @table