Skip to content

Instantly share code, notes, and snippets.

@dermatologist
Forked from patpawlowski/RTF2TXTfn.sql
Created February 22, 2023 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dermatologist/a4626085087d1529ec31cff981c8ccea to your computer and use it in GitHub Desktop.
Save dermatologist/a4626085087d1529ec31cff981c8ccea to your computer and use it in GitHub Desktop.
VERY simple SQL RTF to TXT converter primarily to convert Act notes to plain text
/*
Written by: patpawlowski
Created On: Oct 26, 2015 at 4:51:52 PM
Description: This is a rough attempt to create a funciton to strip the markup from
the Act TBL_NOTE.NOTETEXT field. It appears to work for what I need
but could probably use some work with the escaped characters.
It's not particularly fast but it is faster than other solutions I've come
across. It takes about 4 seconds to parse 2700 records.
File: RTF2TXTfn.sql
*/
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RTF2TXT]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].RTF2TXT
GO
CREATE FUNCTION RTF2TXT
(@In VARCHAR(MAX))
RETURNS VARCHAR(MAX)
AS
BEGIN
If isnull(@In,'') = '' return ''
If @In not like '{\rtf%' return @In
Declare @Len int
Declare @Loc int = 1
Declare @Char char(1) = ''
Declare @PrevChar char(1) = ''
Declare @NextChar char(1) = ''
Declare @InMarkup int = 0
Declare @InBrackets int = -1
Declare @Out varchar(max) = ''
Set @Len = len(@In)
While @Loc < @Len begin
Set @PrevChar = @Char
Set @Char = SUBSTRING(@In, @Loc, 1)
If @Loc < @Len set @NextChar = SUBSTRING(@In, @Loc + 1, 1) else set @NextChar = ''
Set @Loc = @Loc + 1
If @Char = '{' and @PrevChar != '\' begin
Set @InBrackets = @InBrackets + 1
Continue
End
If @Char = '}' and @PrevChar != '\' begin
Set @InBrackets = @InBrackets - 1
Continue
End
If @Char = '\' and @PrevChar != '\' and @NextChar not in ('\','{','}','~','-','_') begin
Set @InMarkup = 1
continue
End
If @Char = ' ' or @Char = char(13) begin
Set @InMarkup = 0
End
If @InMarkup > 0 or @InBrackets > 0 continue
Set @Out = @Out + @Char
End
Set @Out = replace(@Out, '\\', '\')
Set @Out = replace(@Out, '\{', '{')
Set @Out = replace(@Out, '\}', '}')
Set @Out = replace(@Out, '\~', ' ')
Set @Out = replace(@Out, '\-', '-')
Set @Out = replace(@Out, '\_', '-')
WHILE ASCII(@Out) < 33
BEGIN
set @Out = substring(@Out,2,len(@Out))
END
set @Out = reverse(@Out)
WHILE ASCII(@Out) < 33
BEGIN
set @Out = substring(@Out,2,len(@Out))
END
set @Out = reverse(@Out)
RETURN LTRIM(RTRIM(@Out))
End
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment