Health and Fitness

How to Read an HL7 v2 Message: Segments, Fields and Components

A plain guide to the HL7 version 2 message format, from MSH and PID to escape sequences, with an example and how to de-identify test messages safely.

Saizul Amin
Saizul Amin7 Nov 2026 · 4 min read
How to Read an HL7 v2 Message: Segments, Fields and Components

How to Read an HL7 v2 Message: Segments, Fields and Components

HL7 version 2 has been moving data between hospital systems since the 1990s and still carries most admissions, lab results and orders worldwide. Newer standards exist, but if you work on an interface between an EHR, a laboratory system or a radiology system, you will meet v2 messages almost every day. They look cryptic at first and are quite regular once you know the rules.

The layout

A message is a set of segments, each on its own line and ending with a carriage return. Every segment begins with a three character name. MSH, the message header, always comes first. Others include EVN for the event, PID for patient identification, PV1 for the visit, OBR for an observation request and OBX for an individual observation.

Inside a segment, fields are separated by the vertical bar. Fields are referred to by segment and position, so PID-5 is the fifth field of PID, the patient name. A field can have components separated by the caret, repetitions separated by the tilde and subcomponents separated by the ampersand. These four characters, plus the backslash used for escapes, are declared at the start of the header, which is why MSH begins with |^~\&.

Because the separators can also occur in real data, they are escaped. \F\ stands for a bar, \S\ for a caret, \R\ for a tilde, \T\ for an ampersand and \E\ for the backslash itself, while \.br\ marks a line break.

MSH-9 holds the message type and trigger event, such as ADT^A01 for an admission, and MSH-10 holds the control ID that the receiver echoes back in its acknowledgment.

Worked example

The HL7 v2 Message Viewer ships with a two message sample in version 2.5.1. The first is ADT^A01 with control ID MSG00001, sent by EXAMPLE_EHR to LAB_SYS, and it holds MSH, EVN, PID, NK1, PV1 and AL1. The second is ORU^R01 with control ID LAB7781, a result message from LAB_SYS back to EXAMPLE_EHR, with MSH, PID, OBR, three OBX segments and an NTE comment. The summary counts 13 segments and no structure issues.

Expand PID in the tree and PID-5 Patient Name shows SAMPLE^JORDAN^A, which opens into three components: family name, given name and middle initial. PID-3 Patient Identifier List reads MRN123456^^^NORTH_CLINIC^MR, where the first component is the ID, the fourth is the assigning authority and the fifth is the identifier type.

The NTE line in the raw message reads Fasting sample \T\ repeat in 3 months. In the Fields table, NTE-3 Comment reads Fasting sample & repeat in 3 months, because the escape for an ampersand has been decoded.

Now tick De-identify. PID-3 becomes ID0001^^^NORTH_CLINIC^MR, still showing which system issued it, and the name becomes SURNAME0001^TEST. The date of birth 19850412 is reduced to 1985, the street is removed from the address so only LEEDS, the postcode district LS1 and GB remain, and the phone becomes 5550000000. The visit number in PV1-19 becomes ID0003. The NTE text is unchanged, because free text is not scanned.

Structure checks

A message that is broken at the structure level fails before any clinical logic runs, so these checks catch many interface errors early. The viewer reports a missing MSH-9, MSH-10, MSH-11 or MSH-12, segments that a message type normally carries but are absent, dates that do not follow the YYYYMMDD pattern with optional time, an empty PID-3 or PID-5, and an OBX of value type NM whose value is not a number. Pasting a test message with a date written 2026-11-05 and an NM value of high produces two of those warnings straight away, plus notes that an ORU message usually has PID and OBR segments.

Common mistakes

Line breaks lost in copying. Segments end with a carriage return. Some tools join them on one line when you copy from a log. The viewer accepts any line ending, but if a segment runs into the next one, check the source.

Dates with dashes. HL7 v2 dates have no separators. 2026-11-05 is not valid in a DT or DTM field; 20261105 is.

Unescaped separators. A comment containing an ampersand or a caret must use the escape sequences, or the receiver will split the value in the wrong place.

Trusting de-identification blindly. Notes, report text and provider names can still identify a person, and a pseudonym is only as good as the name it replaced. Read the output before it leaves your organisation and follow your local privacy rules.

Saving the result

The JSON download lists each segment with keys such as PID-5 Patient Name, which is handy for test fixtures and documentation. The fields CSV opens in a spreadsheet for side by side comparison, and the de-identified .hl7 file can be replayed into a test system. This is a format tool only; it does not interpret results and is not medical advice.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages