Skip to content

Instantly share code, notes, and snippets.

@patpawlowski
Last active February 14, 2024 16:00
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save patpawlowski/d2ce5e21e02e9fa23f81 to your computer and use it in GitHub Desktop.
Save patpawlowski/d2ce5e21e02e9fa23f81 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
@pfred2
Copy link

pfred2 commented Jan 16, 2019

This has saved my life. Thank you!!!

@DamienAstolfi
Copy link

Très utile, merci.

@KesharaWaidyarathna
Copy link

thank you soooo much😍

@felipedsc
Copy link

It worked like a charm. Thanks.

@ovan2007
Copy link

Thank you. It works perfectly

Copy link

ghost commented Feb 22, 2021

Brilliant stuff, runs very efficient!!

@vasple
Copy link

vasple commented Nov 22, 2022

You sir are a legend! Thank you very much!

@sistemasgrupoidea
Copy link

Thank you very much

@MightyEJ
Copy link

is there any other way around text to rtf?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment