我创建了一个MSSQL Server函数,它将特殊字符(例如:हिन्दीabcde fG#)编码为带有“#”和“;”的unicode号码。作为分隔符。只有非常简单的字符如“abc”才会被编码:
declare @position int, @txt nvarchar(max), @output as varchar(max);
set @position=1;
set @txt = N'हिन्दीabcde fG#';
set @output = '';
while @position <= len(@txt)
begin
declare @t int;
select @t=unicode(substring(@txt,@position,1))
--print '&#'+ CONVERT(nvarchar(5),@t)+';'
if ( (@t between 48 and 57) OR (@t between 65 and 90) or (@t between 97 and 122) )
BEGIN
SET @output = @output + CONVERT(nvarchar(5), substring(@txt,@position,1) );
END
else
BEGIN
SET @output = @output + '#'+ CONVERT(nvarchar(5),@t)+';'
END
set @position = @position+1
end
Print @output
结果是:
2361;#2367;#2344;#2381;#2342;#2368;abcde#32;fG#35;
我需要它来处理ODBC驱动程序并避免使用特殊字符的问题。
但现在我需要回来 - 解码编码的字符。有没有任何智能解决方案,或者我需要至少两个循环,“NCHAR”功能......?
我会尝试构建这样一个函数 - 如果它成功了,我会在这里发布:)
答案 0 :(得分:2)
您可能会发现这种方法更具吸引力。首先,创建一个维护顺序的拆分函数:
CREATE FUNCTION dbo.SplitStringsOrdered
(
@List NVARCHAR(MAX),
@delim NVARCHAR(10)
)
RETURNS TABLE
AS
RETURN
(
SELECT rn, v = LTRIM(RTRIM(SUBSTRING(@List, rn,
CHARINDEX(@delim, @List + @delim, rn) - rn)))
FROM
(
SELECT TOP (8000) rn = ROW_NUMBER() OVER (ORDER BY s1.[object_id])
FROM sys.all_objects AS s1 CROSS JOIN sys.all_objects AS s2
) AS n
WHERE rn <= LEN(@List)
AND SUBSTRING(@delim + @List, rn, LEN(@delim)) = @delim
);
GO
用法:
DECLARE @x NVARCHAR(MAX) = N'#2361;#2367;#2344;#2381;#2342;#2368;abcde#32;fG#35;';
-- need one small adjustment to make the string more split-friendly:
SET @x = REPLACE(@x, '#', ';#');
DECLARE @output NVARCHAR(MAX);
SELECT @output = (SELECT
CASE WHEN v LIKE '#%' THEN NCHAR(REPLACE(v, '#', '')) ELSE v END
FROM dbo.SplitStringsOrdered(@x, ';') AS x
ORDER BY rn FOR XML PATH(''), TYPE).value('.[1]','nvarchar(max)');
SELECT @output;
输出:
हिन्दीabcde fG#
答案 1 :(得分:0)
我已用此查询解决了问题:
declare @position int, @txt nvarchar(max), @output as nvarchar(max), @buffer as varchar(max);
set @position=1;
set @txt = '#2361;#2367;#2344;#2381;#2342;#2368;abcde#32;fG#35;';
set @output = '';
set @buffer = '';
while @position <= len(@txt)
begin
declare @t varchar(max);
select @t=(substring(@txt,@position,1))
if ( len(@buffer) = 0 and @t <> '#' and @t <> ';')
BEGIN
-- Append simple chars, which were not encoded
Print 'Hänge den String ganz normal an den Output: ' + @t
SET @output = @output + @t;
END
ELSE
BEGIN
if ( @t = '#' )
BEGIN
Print 'Raute wurde erkannt: #';
SET @buffer = '#';
END
else if ( @t = ';' )
BEGIN
SET @buffer = REPLACE( @buffer, '#' , '' );
Print 'Umwandeln: ' + @buffer
SET @output = @output + isnull( NCHAR(@buffer) , '');
SET @buffer = '';
END
else
BEGIN
Print 'Ganzzahl an den Buffer anhängen: ' + @t;
SET @buffer = @buffer + @t;
END
END
set @position = @position+1
end
Print @output