-
Notifications
You must be signed in to change notification settings - Fork 674
/
udf_RTF2Text.sql
106 lines (88 loc) · 2.87 KB
/
udf_RTF2Text.sql
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
IF OBJECT_ID('[dbo].[udf_RTF2Text]', 'FN') IS NULL
EXECUTE('CREATE FUNCTION [dbo].[udf_RTF2Text] (@input INT) RETURNS INT AS BEGIN RETURN 1 END');
go
ALTER FUNCTION [dbo].[udf_RTF2Text]
(
@rtf nvarchar(max)
)
RETURNS nvarchar(max)
AS
/*
Author: NightWizzard
Source: http://www.codeproject.com/Tips/821281/Convert-RTF-to-Plain-Text-Revised-Again
SQLServer version: 2008/2012/2014
SELECT [<YourRTFColumnNameHere>]
, [dbo].[RTF2Text]([<YourRTFColumnNameHere>]) AS [TextFromRTF]
FROM [dbo].[<YourTableNameHere>]
*/
BEGIN
DECLARE @Pos1 int;
DECLARE @Pos2 int;
DECLARE @hex varchar(316);
DECLARE @Stage table
(
[Char] char(1),
[Pos] int
);
INSERT @Stage
(
[Char]
, [Pos]
)
SELECT SUBSTRING(@rtf, [Number], 1)
, [Number]
FROM [master]..[spt_values]
WHERE ([Type] = 'p')
AND (SUBSTRING(@rtf, Number, 1) IN ('{', '}'));
SELECT @Pos1 = MIN([Pos])
, @Pos2 = MAX([Pos])
FROM @Stage;
DELETE
FROM @Stage
WHERE ([Pos] IN (@Pos1, @Pos2));
WHILE (1 = 1)
BEGIN
SELECT TOP 1 @Pos1 = s1.[Pos]
, @Pos2 = s2.[Pos]
FROM @Stage s1
INNER JOIN @Stage s2 ON s2.[Pos] > s1.[Pos]
WHERE (s1.[Char] = '{')
AND (s2.[Char] = '}')
ORDER BY s2.[Pos] - s1.[Pos];
IF @@ROWCOUNT = 0
BREAK
DELETE
FROM @Stage
WHERE ([Pos] IN (@Pos1, @Pos2));
UPDATE @Stage
SET [Pos] = [Pos] - @Pos2 + @Pos1 - 1
WHERE ([Pos] > @Pos2);
SET @rtf = STUFF(@rtf, @Pos1, @Pos2 - @Pos1 + 1, '');
END
SET @Pos1 = PATINDEX('%\cf[0123456789][0123456789 ]%', @rtf);
WHILE @Pos1 > 0
SELECT @Pos2 = CHARINDEX(' ', @rtf, @Pos1 + 1),
@rtf = STUFF(@rtf, @Pos1, @Pos2 - @Pos1 + 1, ''),
@Pos1 = PATINDEX('%\cf[0123456789][0123456789 ]%', @rtf);
SELECT @rtf = REPLACE(@rtf, '\pard', ''), @rtf = REPLACE(@rtf, '\par', ''),
@rtf = CASE WHEN LEN(@rtf) > 0 THEN LEFT(@rtf, LEN(@rtf) - 1) ELSE @rtf END;
SELECT @rtf = REPLACE(@rtf, '\b0 ', ''), @rtf = REPLACE(@rtf, '\b ', '');
SELECT @rtf = STUFF(@rtf, 1, CHARINDEX(' ', @rtf), '');
WHILE (Right(@rtf, 1) IN (' ', CHAR(13), CHAR(10), '}'))
BEGIN
SELECT @rtf = SUBSTRING(@rtf, 1, (LEN(@rtf) - 1));
IF LEN(@rtf) = 0 BREAK
END
SET @Pos1 = CHARINDEX('\' + CHAR(39), @rtf);
WHILE (@Pos1 > 0)
BEGIN
IF @Pos1 > 0
BEGIN
SET @hex = '0x' + SUBSTRING(@rtf, @Pos1 + 2, 2);
SELECT @rtf = REPLACE(@rtf, SUBSTRING(@rtf, @Pos1, 4),
CHAR(CONVERT(int, CONVERT (binary(1), @hex,1))))
SET @Pos1 = CHARINDEX('\''', @rtf)
END
END
RETURN @rtf;
END