Blogs Home
Monday, July 31, 2017 12:00 PM

SQL Tip: How to Remove MTDD-style Formatting from TextLines

Written by Thomas Harlan, Jim McGrath; Reporting Services Team - iatricSystems

This example is based on how discharge text data is formatted in the MEDITECH MAGIC Data Repository (DR).

In the continuing saga of trying to recreate patient discharge instructions from the DR, I discovered that among the complications is detecting where line breaks should actually go.

Set Up:

MEDITECH has a format, MTDD, which sort of replaces Word formatting. This can be detected in the Format column of the PcmDocPatientDischargeTextRoot table. MTDD embeds printer commands and control characters in the text in the MAGIC data structures, which is all well and good.

However, two problems are introduced when the data is transferred to DR.

First, for whatever reason, MEDITECH replaces all non-printable characters with spaces. This yields print commands in the following form:

<Space><Font><Space><Size>[<Style>]d<Space>

Where <Font> is one of nine supported fonts, <Size> is an integer (0 – 9) representing a ratio for the text size (but it is not the ratio itself — that’s hidden elsewhere), and <Style> = B, BI or I.

Supported Fonts in MTDD

Arial
CG Omega
Clarendon Condensed
Courier
Garamond
Times New Roman
Univers
Verdana
Wingdings

Here is the first of two SQL functions:

IatricMTDD2Text()

Which strips these print commands out of a line of MTDD text.

Line breaks present a second problem. MEDITECH text rows do not necessarily correspond with the expected line breaks on the printed documents. MAGIC allows 60 characters for each line of text, while the actual text lines (stored with this additional formatting) may far exceed that limit. So MEDITECH stores the carriage return in the text (which in turn gets converted to a space in DR).

The second of the two functions:

IatricDischargeText()

Makes a pretty good (if I do say so myself, which I do) attempt to cobble the lines back together. This process assumes a 75-character line width. The function returns a table of data rows, after processing them to strip out the extra formatting and restructure them into 75-character sections.

Note that the function processes two specific tables:

livedb.dbo.PcmDcmts
livedb.dbo.PcmDocPatientDischargeText

Which would need to exist in your livedb and be populated with data.

Using these two functions in tandem, like so:

-- Show discharge text for last 10 days of (mostly Inpatient) discharges
SELECT  DIS.SourceID
       ,DIS.VisitID
       ,DIS.DischargeDateTime
       ,ADV.Status
       ,IDT.SeqID
       ,IDT.TextLine
FROM    livedb.dbo.AdmDischarge DIS
        LEFT JOIN livedb.dbo.AdmVisits ADV ON ( ADV.SourceID = DIS.SourceID AND ADV.VisitID = DIS.VisitID )
        -- When calling a table valued function, it's useful to do an OUTER APPLY to bring it in.
        OUTER APPLY zcus.dbo.IatricDischargeText( DIS.SourceID,DIS.VisitID ) IDT               

WHERE   DIS.DischargeDateTime BETWEEN GETDATE()-10 AND GETDATE()
ORDER BY 2,4
;

Produces the discharge text in a format we can then use in SSRS, or in a file export.

What we gain: Pretty decent looking text. Not quite perfect, but pretty good. What we lose: Centered headers. Bold and italics. Resized text.

Extra Credit:

If you run across another MTDD-formatted text structure in your DR, you can make a new version of IatricDischargeText for that structure as well…

Extra Extra Credit:

Adapt the IatricMTDD2Text() function to be IatricMTDD2HTML() and convert those missing B, BI, and I codes into and pairs! Then the text lines can be fed into an SSRS field set to display HTML formatting.

If you need more help…

Our Report Writing team can help you fix reports, create new ones, make old ones faster, and much more. Simply reach out to your Iatric Systems Account Executive or our NPR report writing team at reportwriting@iatric.com to discuss how we can help support your team!

RW-HELP-Button-Blue.png