Data types
How a field's data type determines its structure and the interpretation of its value.
Every field in an HL7v2 message has a data type. The data type defines how the field's value is written: whether it is a single value or is divided into components, what each component means, and how each part is formatted.
The segment definition identifies what a field represents, and the data type defines how to read its value. A field cannot be interpreted correctly without both.
Both definitions are needed because HL7v2 is not a self-describing format. A JSON or XML document labels each value with a name, so a reader can see what a value is from the document itself. An HL7v2 message contains only values and delimiters: the value 19700101 appears in field 7 of the PID segment, and nothing in the message states that it is a date of birth, or a date at all. The meaning of each value comes from definitions outside the message: the segment definition and the data type in the version of the standard that the message declares, narrowed by the interface's profile. A destination that applies different definitions from the source reads the same characters differently, and no error is reported.
Example
The following PID segment comes from an admission message:
PID|1||12345^^^Hospital^MR||Doe^John^A^^Dr.||19700101|MPID-5, the patient name, contains Doe^John^A^^Dr.. The component delimiters divide the value into parts, but the segment definition does not assign meaning to those parts. That meaning comes from the data type of PID-5, XPN (extended person name), which defines a fixed component order: family name, given name, further given names, suffix, prefix. Under that definition the value resolves to Dr. John A. Doe, with no suffix.
Interpreting a field therefore takes two lookups: the field's data type in the segment definition, then that data type's structure in the data type reference. Applied to every field in the segment:
| Field | Data type | Value | Reading |
|---|---|---|---|
PID-1 | SI | 1 | A sequence number. This is the first PID segment. |
PID-3 | CX | 12345^^^Hospital^MR | An identifier: ID 12345, no check digit (components 2 and 3 are empty), assigned by Hospital, of type MR (medical record number). |
PID-5 | XPN | Doe^John^A^^Dr. | A person name: family name Doe, given name John, middle initial A, no suffix, prefix Dr.. |
PID-7 | DTM | 19700101 | A timestamp, here with day precision: born 1 January 1970. |
PID-8 | CWE | M | A coded value. The table bound to PID-8 defines M as male. |
Reusability
Data types are defined once and reused across the standard. The next-of-kin name in NK1-2, the attending physician in PV1-7, and the ordering provider in OBR-16 all use the XPN structure, so logic that interprets one person name interprets all of them. Addresses, identifiers, telephone numbers, and timestamps follow the same principle. Instead of specifying the structure of several thousand fields individually, the standard defines roughly ninety data types.
This reusability is also what makes the component and sub-component levels of a message consistent. A field has components only when its data type is composite, and those components are exactly the ones the data type defines.
Categories
Data types fall into two categories: primitive types, which hold a single value, and composite types, which hold several values called components.
Primitives
A primitive type holds a single value, and its definition specifies how that value is written. For example, ST is a string, NM is a number, and DTM is a date and time.
The format of a primitive can carry meaning. A DTM value is written from the year down to fractions of a second, and the source includes only the parts it knows:
| Value | Precision |
|---|---|
1970 | Year |
19700101 | Day |
20230630120000 | Second |
20230630120000-0500 | Second, with a time-zone offset |
A date of birth sent as 1970 therefore means that only the year is known. A destination that stores it as 19700101 records a day that the source never provided.
A primitive type also limits what a field can hold. NM accepts only digits, a sign, and a decimal point, so a laboratory result such as <5 cannot be sent in an NM field. The standard provides a separate type, SN (structured numeric), for values with a comparator.
Composites
A composite type holds several components in a fixed order, separated by ^, and each component has its own meaning and its own data type. The person-name type XPN, for example, begins with the family name and the given name, so Doe^John is family name Doe and given name John.
Two kinds of composite carry much of the information in an HL7v2 message: identifiers, which state who or what a value refers to, such as a patient or an order, and codes, which state what a value means, such as a diagnosis or a laboratory test.
Codes are central because healthcare describes clinical information with standard coding systems, also called terminologies or ontologies, in which each concept has a code with a precise, shared meaning: LOINC (opens in a new tab) for laboratory tests and observations, SNOMED CT (opens in a new tab) for clinical findings, ICD-10 for diagnoses, and RxNorm for medications. A LOINC code identifies the same test in every system that uses LOINC, so a result coded by a laboratory can be interpreted correctly by an electronic health record, a public-health registry, or a research database. This shared meaning is what makes coded data interoperable, in addition to the shared syntax of the message.
Codes and identifiers both depend on their source. A code has meaning only within the coding system that defines it, and an identifier is unique only within the organization that issued it. HL7v2 composite types therefore carry the source together with the value: the coding system with each code, and the issuing organization with each identifier. A destination can then interpret each value correctly without making assumptions about the system that sent it.
Identifiers
A hospital assigns each patient a medical record number, such as 12345, but another hospital can assign the same number to a different patient. A number on its own therefore does not identify a patient. The identifier type CX carries the number together with who issued it and what kind of number it is:
| Component | Example | Meaning |
|---|---|---|
| 1 | 12345 | The identifier itself. |
| 4 | Hospital | The organization that issued it, called the assigning authority. |
| 5 | MR | The kind of identifier, here a medical record number. |
In a message, the components are written in order and separated by ^, which gives 12345^^^Hospital^MR. Components 2 and 3, a check digit and the scheme used to calculate it, are empty in this example, so three delimiters appear in a row.
Orders, specimens, and clinicians are identified in the same way. A destination that reads only the number can merge two patients who have the same number at different hospitals.
Codes
A code has meaning only within the coding system that defines it, and the same code can appear in more than one coding system. The coded type CWE carries the code together with a description and the name of its coding system: 2345-7^Glucose^LN is code 2345-7, described as "Glucose", from LOINC.
Diagnoses, procedures, test results, and administrative values such as a patient's sex are coded in the same way. A destination that reads only the code can interpret a code from one coding system as a code from another. Tables describes coding systems in detail.
Sub-components
Because each component has a data type of its own, a component can itself be composite. The fourth component of CX, the assigning authority, has data type HD, which has three parts: a namespace, a universal identifier, and the type of that identifier. The component delimiter is already in use at the field level, so the parts of a nested type are separated by the sub-component delimiter &:
PID|1||12345^^^Hospital&1.2.840.114350&ISO^MRThe assigning authority is now Hospital&1.2.840.114350&ISO: namespace Hospital, universal identifier 1.2.840.114350, identifier type ISO (an object identifier). This is the deepest level the standard allows. A sub-component is always primitive, so a parser never needs more than three levels of splitting below the field.
Variable types
A small number of fields have no fixed data type. The most important is OBX-5, the observation value. A result can be a number, a short string, a coded finding, a date, or an embedded document, and no single type accommodates all of them. The standard resolves this by letting OBX-2 declare the data type that OBX-5 uses in that segment:
OBX|1|NM|2345-7^Glucose^LN||95|mg/dL|70-99|N|||F
OBX|2|CWE|883-9^ABO group^LN||58460004^Blood group O^SCT||||||FThe first observation is a glucose of 95 mg/dL, a plain number. The second is a blood group, a coded value identified by its SNOMED CT code. A parser must read OBX-2 before it can determine whether to treat OBX-5 as a single value or split it into components. Other fields follow the same pattern, such as MFE-4, whose type is declared in MFE-5, but OBX-5 is the one that most interfaces encounter.
Version changes
Data types have changed more between versions than any other part of the standard. Timestamps moved from TS to DTM in version 2.6. The coded type CE was replaced by CWE and CNE in the same release, and version 2.7 retyped many simple coded fields, including PID-8, to CWE. Components have been appended to XPN, XAD, and CX in most releases, which is why a version 2.8 person name has fifteen components and a version 2.3 name has eight.
The changes are additive, so a value written in an older form remains valid in the newer one. F is a valid CWE, because the code is the first component and the others are optional. Compatibility problems arise in the other direction: a destination built for an older version may not expect the components that a newer source populates. Versioning discusses what the standard guarantees.
Trade-offs
A shared set of data types limits the number of structures an implementer must learn. The definition of XPN applies to every person name in the standard, and the definition of CX applies to every identifier. Defining components separately for each field would have produced a larger and less consistent specification.
The cost is that the set of types is large and uneven. Several types overlap, and the composite types have accumulated components that are rarely populated. The XPN definition, for example, lists fifteen components, although most interfaces populate only the first few. The reference defines every component; the components that a particular interface uses are specified in its profile.
Further reading
- Data types reference: the complete definition of every data type and its components for version
2.8. - Tables: coded types and the vocabularies they draw from.
- Message structure: the delimiters that separate components and sub-components.