所有
我正在尝试用URL替换字符串中的特殊字符 编码它们对应的值。下面是我的一些示例代码 一直在努力。
感谢您的帮助。
create table #url_encoding_lookup(character varchar(10), code varchar (20))
insert into #url_encoding_lookup (character, code)
values
('!', '%21'),
('"', '%22'),
('#', '%23'),
('$', '%24'),
('%', '%25'),
('&', '%26'),
('''', '%27'),
('(', '%28'),
(')', '%29'),
('*', '%2A'),
('+', '%2B'),
(',', '%2C'),
('-', '%2D'),
('.', '%2E'),
('/', '%2F')
Create table #data
(string varchar (200))
insert into #data
values
('Jim (BoB)'),
('Will''s Place'),
('Auto-Mart')
select * from #data
select * from #url_encoding_lookup
desired results would be
Jim %28Bob%29
Will%27s Place
Auto%2DMart
答案 0 :(得分:0)
ALTER FUNCTION [dbo].[udf_ReplaceYouCoded]
(
@the_string nvarchar(max)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
declare @temp_field nvarchar(max)
create table #url_encoding_lookup(character varchar(10), code varchar (20))
insert into #url_encoding_lookup (character, code)
values
('!', '%21'),
('"', '%22'),
('#', '%23'),
('$', '%24'),
('%', '%25'),
('&', '%26'),
('''', '%27'),
('(', '%28'),
(')', '%29'),
('*', '%2A'),
('+', '%2B'),
(',', '%2C'),
('-', '%2D'),
('.', '%2E'),
('/', '%2F')
declare @x as int
set @x = 1
--LOOP @the_string
while @i < len(@the_string)
if(substring(@the_string,x,1) = (select character from #url_encoding_lookup where character = substring(@the_string,x,1)))
begin
@temp_field = @temp_field + (select code from #url_encoding_lookup where character = substring(@the_string,x,1))
end
else
begin
@temp_field = @temp_field + substring(@the_string,x,1)
end
select @x = @x + 1
end
RETURN @temp_field
END
答案 1 :(得分:0)
创建程序
BEGIN
DECLARE _end BOOLEAN DEFAULT FALSE;
DECLARE _result CHAR(200) DEFAULT str;
DECLARE _find VARCHAR(32);
DECLARE _replace VARCHAR(32);
DECLARE _cur CURSOR FOR SELECT _character, _code FROM url_encoding_lookup;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET _end = TRUE;
OPEN _cur;
_loop: LOOP
FETCH _cur INTO _find, _replace;
IF _end THEN
LEAVE _loop;
END IF;
SET _result = REPLACE(_result, _find, _replace);
END LOOP _loop;
CLOSE _cur;
RETURN _result;
END
然后
SELECT _replace_chars(name) FROM `data`
结果
Jim %28BoB%29
Will%27s Place
Auto%2DMart
答案 2 :(得分:0)
ALTER function [dbo].[udf_ReplaceSpecialChars]
(@s varchar(256))
returns varchar(256)
as
begin
-- declare @s varchar(256) set @s = 'Jim (P)' --test
if @s is null
return null
declare @s2 varchar(256)
set @s2 = '' --set variable to empty string. ready to recieve values
declare @l int
set @l = len(@s) --determin the number of characters in @s
declare @p int
set @p = 1 --set beginning string position
while @p <= @l begin
declare @c int
set @c = ascii(substring(@s, @p, 1)) --find the ascii number for 1st
character
declare @nc varchar(256)
set @nc = (select code from url_encoding_lookup where ascii_code = @c) --
get corresponding URL encoding string from lookup table
if @c between 33 and 47 or @c between 58 and 64 or @c between 91 and 96
or @c between 123 and 255 --when looping through each character, if special
character
set @s2 = @s2 + @nc --then use string from lookup table
else if @c = 32 or @c between 48 and 57 or @c between 65 and 90 or @c
between 97 and 122 --if character is not special
set @s2 = @s2 + char(@c) --then find char value of character
set @p = @p + 1 --set position to next charachter for loop to look at
end
if len(@s2) = 0
return null
return @s2 --return rebuilt string
end
答案 3 :(得分:0)
您可以使用以下正则表达式[\u0100-\uffff]替换特殊字符
select regexp_replace(column, '[\u0100-\uffff]', '')