Overall Structure
An HL7 V2 message consists of segments, which consist of fields, which may contain components and subcomponents. Understanding this hierarchical structure is essential for parsing and creating messages correctly. Each level of the hierarchy uses different delimiter characters, allowing complex data to be nested within a single text-based message. This design enables HL7 V2 to represent rich clinical data while remaining human-readable and easy to transmit.
Delimiters
Delimiters are the backbone of HL7 V2 message parsing. These special characters allow receivers to split messages into their constituent parts without ambiguity. The field separator appears in MSH-1 (the fourth character of every message), while the remaining encoding characters are defined in MSH-2. Understanding these delimiters is essential because using them incorrectly within data values will corrupt your messages.
HL7 V2 uses five special characters as delimiters:
| Character | Name | Purpose | Position in MSH |
|---|---|---|---|
| ` | ` | Field Separator | Separates fields within a segment |
^ | Component Separator | Separates components within a field | MSH-2 position 1 |
~ | Repetition Separator | Separates repeated fields | MSH-2 position 2 |
\ | Escape Character | Escapes special characters | MSH-2 position 3 |
& | Subcomponent Separator | Separates subcomponents | MSH-2 position 4 |
Example Message Structure
The following example demonstrates a complete ADT (Admission, Discharge, Transfer) message with four segments. Each segment begins with a three-character identifier and contains pipe-delimited fields. When examining real messages, you will notice that empty fields are represented by consecutive pipes (||), and component separators (^) appear within complex fields like patient names and addresses.
MSH|^~\&|SENDING_APP|SENDING_FACILITY|RECEIVING_APP|RECEIVING_FACILITY|20231117120000||ADT^A01|MSG00001|P|2.5
EVN|A01|20231117120000
PID|1||123456^^^HOSPITAL^MR||DOE^JOHN^A||19800115|M|||123 MAIN ST^^CITY^ST^12345^USA|||||||123-45-6789
PV1|1|I|2000^2012^01||||004777^SMITH^JOHN^^^DR|||SUR||||ADM|A0
Segment Structure
Segments are the building blocks of HL7 messages, each representing a distinct category of information. Every segment begins with a three-character identifier that tells the parser what type of data follows. When creating or parsing messages, you must understand segment boundaries to correctly extract and populate clinical data.
Each segment:
- Starts with a 3-character segment ID (e.g., MSH, PID, OBR)
- Followed by field separator (|)
- Contains numbered fields starting at 1
- Ends with carriage return (CR, \r) or carriage return + line feed (CRLF, \r\n)
Special Segments
MSH (Message Header)
The MSH segment is the most critical segment in any HL7 V2 message as it must always be the first segment and is required in every message. It defines essential message characteristics including the sender, receiver, message type, unique control ID, and HL7 version. The receiving system uses MSH data to determine how to parse and route the message, making accurate MSH population essential for successful message exchange.
MSH|^~\&|SENDING_APP|SENDING_FACILITY|RECEIVING_APP|RECEIVING_FACILITY|20231117120000||ADT^A01|MSG00001|P|2.5
Fields breakdown:
- MSH-1: Field separator (|)
- MSH-2: Encoding characters (^~&)
- MSH-3: Sending application
- MSH-4: Sending facility
- MSH-5: Receiving application
- MSH-6: Receiving facility
- MSH-7: Date/time of message
- MSH-9: Message type (ADT^A01)
- MSH-10: Message control ID (unique)
- MSH-11: Processing ID (P=Production, T=Test, D=Debug)
- MSH-12: Version ID (2.5)