实际遇到的问题
1. 将unicode
转换成字符
- 在
dbo
模式下,创建函数
sql
ALTER FUNCTION dbo.DecodeUnicode(@unicodeStr NVARCHAR(MAX))
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE @result NVARCHAR(MAX) = N'';
DECLARE @pos INT = 1;
DECLARE @len INT = LEN(@unicodeStr);
WHILE @pos <= @len
BEGIN
IF SUBSTRING(@unicodeStr, @pos, 2) = N'\u'
BEGIN
SET @result += NCHAR(
CONVERT(
INT,
CONVERT(
VARBINARY
, '0x' + SUBSTRING(@unicodeStr, @pos + 2, 4)
, 1
)
)
);
SET @pos += 6; -- 跳过已处理的 Unicode 转义字符
END
ELSE
BEGIN
SET @result += SUBSTRING(@unicodeStr, @pos, 1);
SET @pos += 1;
END
END
RETURN @result;
END;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
- 调用函数
sql
SELECT
'\u5f20\u4e09' AS unicode_data
, dbo.DecodeUnicode('\u5f20\u4e09') AS deunicode_data
1
2
3
2
3
unicode_data | deunicode_data |
---|---|
\u5f20\u4e09 | 张三 |