MSH-2 Encoding Characters Must Be Exactly 4 Characters
MSH-2 declares the component separator, repetition separator, escape character, and subcomponent separator, in that order. It must be exactly four characters long.
HL7v2 · Error
The error
MSH-2 (encoding characters) must be exactly 4 charactersWhy it happens
A truncated MSH-2, an extra delimiter, or a backslash that was unescaped or doubled by a programming-language string literal. JSON or shell escaping commonly mangles the backslash in ^~\&.
How to fix it
Set MSH-2 to the four standard characters: ^~\& (component, repetition, escape, subcomponent). Watch out for code that doubles the backslash. The raw on-wire bytes must be exactly those four characters.
Example
The snippet below triggers the error:
MSH|^~&|SENDAPP|SENDFAC|RCVAPP|RCVFAC|20240115||ADT^A01|MSG001|P|2.5The corrected version:
MSH|^~\&|SENDAPP|SENDFAC|RCVAPP|RCVFAC|20240115||ADT^A01|MSG001|P|2.5Frequently asked questions
What do the four characters mean?
In order: ^ component separator, ~ repetition separator, \ escape character, & subcomponent separator. All four must be present and distinct so the parser can resolve nested structures unambiguously.
Related
- Open the HL7v2 Validator
- All validation errors
- Message Does Not Begin With an MSH Segment
- MSH-1 Field Separator Must Be Exactly One Character
- Delimiter Characters Are Not Unique
- Required Segment Is Missing
- Required Field Is Missing or Empty
- Segment Appears More Times Than Allowed
- Segment Is Not Defined in This HL7v2 Version
- Invalid Message Type and Trigger Event Combination