HL7
Split, validate, and pull fields out of HL7 v2.x clinical messages such as ADT and ORU feeds
- Connector software
- Free and open source
- Service credentials
- Not needed
- Query Farm hosting
- Available · $10/month organization minimum
Use with an agent
Install the Orchard skill in your project to help your agent find connectors, configure them, and verify SQL results.
npx skills add Query-farm/vgi-skills --skill orchard
Copy this request into your coding agent to get started.
Use https://query.farm/products/orchard/connectors/hl7/index.md to help me query HL7 with DuckDB or Haybarn 2.0.1 and VGI protocol 3.4. Ask only for missing details about the result I need or whether I want local execution, self-hosting or Query Farm hosting. Check the connector's actual hosting availability, credentials and input requirements. Read one format of each needed reference and the exact object declarations before writing SQL. Use a released connector, and distinguish a saved example result from anything you actually execute. If you cannot run the query, tell me what remains to verify.
Read an HL7 admission message as SQL rows
Example output · captured
- Connector release
- v0.5.0
- Engine
- 2.0.1
- VGI protocol
- 3.4.0
SELECT seq, segment, field_count
FROM hl7.main.hl7_segments(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I')
ORDER BY seq
| seq | segment | field_count |
|---|---|---|
| 1 | MSH | 12 |
| 2 | EVN | 2 |
| 3 | PID | 8 |
| 4 | PV1 | 2 |
Quick start
Choose where to run HL7, then follow the steps below.
The connector runs alongside DuckDB or Haybarn. No Query Farm account is needed.
1. Install the vgi extension
Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;
INSTALL vgi FROM community;
LOAD vgi;
2. Attach the connector
Attach the signed image
Needs Docker, Podman or another OCI runtime. The extension runs the container and talks to it over stdio.
ATTACH 'hl7' AS hl7 (
TYPE vgi,
LOCATION 'oci://ghcr.io/query-farm/vgi-hl7:0.5.0'
);
3. Run a query
SELECT seq, segment, field_count
FROM hl7.main.hl7_segments(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I')
ORDER BY seq
Run the connector on a server you manage. You operate the service and choose who can connect.
1. Deploy the connector on your server
Follow the connector’s deployment instructions. Use its supported HTTP transport, or launch it remotely over SSH. The connection guide explains both options.
2. Install the vgi extension
Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;
INSTALL vgi FROM community;
LOAD vgi;
3. Attach the connector
For an HTTP deployment, replace the example URL with your endpoint. Add the authentication options your deployment requires. For SSH, use the SSH connection instructions.
ATTACH 'hl7' AS hl7
(TYPE vgi, LOCATION 'https://your-connector.example.com');
4. Run a query
SELECT seq, segment, field_count
FROM hl7.main.hl7_segments(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I')
ORDER BY seq
Query Farm operates HL7 for your team. Hosting has a $10 monthly minimum per organization; usage counts toward it. From $2.97 per instance-month ($10 monthly minimum).
1. Create your hosted connector
Open the console, choose your organization and deployment size, and add any service credentials there.
2. Install the vgi extension
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;
INSTALL vgi FROM community;
LOAD vgi;
3. Attach your deployment
Copy the ATTACH statement from your deployment in the console and run it in DuckDB or Haybarn. Use hl7 as the catalog alias for the query below.
4. Run a query
SELECT seq, segment, field_count
FROM hl7.main.hl7_segments(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I')
ORDER BY seq
Technical details
- Verified connector release
- v0.5.0
- Engine
- 2.0.1
- VGI protocol
- 3.4.0
- Verified on
- December 1, 2026
Pin a version for production
When running the connector yourself, keep a specific release until you’re ready to upgrade. Replace the ATTACH statement above with this one and follow the same extension and credential setup.
Attach the signed image
Needs Docker, Podman or another OCI runtime. The extension runs the container and talks to it over stdio.
ATTACH 'hl7' AS hl7 (
TYPE vgi,
LOCATION 'oci://ghcr.io/query-farm/vgi-hl7:0.5.0'
);
Overview
HL7 v2.x clinical message parsing (pure-Java, no HAPI)

Parse, validate, and query HL7 v2.x pipe-delimited clinical messages directly in DuckDB SQL — turn raw ADT, ORU, and ORM feeds into queryable rows over Apache Arrow, with no ETL pipeline and no heavyweight HL7 library to install.
This extension is built for healthcare data engineers, interoperability and integration teams, and anyone who needs to ingest HL7 v2 messages from EHR/EMR systems, interface engines, or message archives and analyze them with plain SQL. HL7 v2 is the most widely deployed healthcare messaging standard in the world, and its segment/field/component/subcomponent layout is notoriously fiddly to parse by hand. Reach for it whenever a clinical feed arrives as raw pipe-delimited text and you want to JOIN, filter, and aggregate its fields the same way you would any other table — rather than pre-processing the messages in an external language first.
The core concepts mirror the HL7 v2 grammar. A message is a sequence of carriage-return–separated segments (MSH, EVN, PID, PV1, OBX, ...), each of which is a list of fields; a field can carry repetitions and decomposes further into components and subcomponents. Individual elements are addressed by an HL7 location path such as PID-5.1 (component 1 of field 5 of the PID segment) or MSH-9, optionally with a segment repetition like DG1[2]-3. Indices are 1-based per HL7 convention.
Under the hood the parser is a compact, pure-Java (pure-JDK) implementation — there is no HAPI dependency, keeping the worker MIT-licensed and dependency-light. It is fully MSH-driven: the field separator (MSH-1) and encoding characters (MSH-2: component, repetition, escape, and subcomponent delimiters) are read from each message rather than assumed, so non-standard delimiters are handled correctly. Malformed input never crashes the worker — it simply parses as invalid and yields no rows, so the functions are safe to run across dirty, mixed-quality feeds.
The format and message grammar follow the official HL7 v2 standard published by Health Level Seven International; see the HL7 v2 product brief and documentation for the canonical segment, field, and message-type definitions. Source code, examples, and issues live in the Query-farm/vgi-hl7 repository.
Catalog
Everything hl7 exposes once attached — read straight from the worker's
released build.
main 1 view · 7 functions
HL7 v2.x message-parsing functions.
Schema examples (9)
Split an admit message into its segments, in message order.
SELECT seq, segment, field_count
FROM hl7.main.hl7_segments(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I')
ORDER BY seq;
| seq | segment | field_count |
|---|---|---|
| 1 | MSH | 12 |
| 2 | EVN | 2 |
| 3 | PID | 8 |
| 4 | PV1 | 2 |
Explode the PID segment into long-format field rows.
SELECT segment, field, repetition, value
FROM hl7.main.hl7_fields(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I')
WHERE segment = 'PID'
ORDER BY field, repetition;
Extract the patient family name (component 1 of PID-5) by location.
SELECT hl7.main.hl7_get(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I', 'PID-5.1') AS family_name;
Read the message type (MSH-9) for routing a mixed feed.
SELECT hl7.main.hl7_message_type(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I') AS message_type;
5 more examples
Read the HL7 version id (MSH-12) the message claims to conform to.
SELECT hl7.main.hl7_version(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I') AS version;
Read the message control id (MSH-10) used for ACK correlation and dedupe.
SELECT hl7.main.hl7_message_control_id(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I') AS control_id;
Validate that a well-formed HL7 v2 message parses.
SELECT hl7.main.is_valid_hl7(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I') AS ok;
Turn a table of raw messages into an ordered message log: skip malformed rows with is_valid_hl7, read the send time (MSH-7) with hl7_get as a TIMESTAMP, and add the control id, message type, and version from the MSH accessors (here: MSG00001 ADT^A01 at 12:00, then MSG00002 ORU^R01 at 12:30).
SELECT strptime(hl7.main.hl7_get(message, 'MSH-7'), '%Y%m%d%H%M%S') AS sent_at,
hl7.main.hl7_message_control_id(message) AS control_id,
hl7.main.hl7_message_type(message) AS message_type,
hl7.main.hl7_version(message) AS version
FROM hl7.main.example_messages
WHERE hl7.main.is_valid_hl7(message)
ORDER BY sent_at, control_id;
Flatten an ORU^R01 lab report into one row per OBX result (test name, numeric value, units, reference range, abnormal flag) by pivoting hl7_fields on segment_rep, stamped with the patient MRN from hl7_get('PID-3.1') (two rows: White Blood Cell Count 7.2 and Hemoglobin 14.1 for MRN 10001).
SELECT hl7.main.hl7_get(E'MSH|^~\\&|LAB|HOSP|EHR|HOSP|20240101123000||ORU^R01|MSG00002|P|2.5\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rOBR|1||ORD123|CBC^Complete Blood Count\rOBX|1|NM|WBC^White Blood Cell Count||7.2|10*3/uL|4.0-11.0|N\rOBX|2|NM|HGB^Hemoglobin||14.1|g/dL|13.5-17.5|N', 'PID-3.1') AS mrn,
f.segment_rep + 1 AS obx_set_id,
split_part(max(f.value) FILTER (WHERE f.field = 3), '^', 2) AS test,
TRY_CAST(max(f.value) FILTER (WHERE f.field = 5) AS DOUBLE) AS result,
max(f.value) FILTER (WHERE f.field = 6) AS units,
max(f.value) FILTER (WHERE f.field = 7) AS reference_range,
max(f.value) FILTER (WHERE f.field = 8) AS abnormal_flag
FROM hl7.main.hl7_fields(E'MSH|^~\\&|LAB|HOSP|EHR|HOSP|20240101123000||ORU^R01|MSG00002|P|2.5\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rOBR|1||ORD123|CBC^Complete Blood Count\rOBX|1|NM|WBC^White Blood Cell Count||7.2|10*3/uL|4.0-11.0|N\rOBX|2|NM|HGB^Hemoglobin||14.1|g/dL|13.5-17.5|N') AS f
WHERE f.segment = 'OBX'
GROUP BY f.segment_rep
ORDER BY obx_set_id;
example_messages
Curated, browsable registry of ready-to-use sample HL7 v2 messages.
List the built-in sample messages, richest first, showing their type, trigger event, and segment count.
SELECT name, message_type, trigger_event, segment_count
FROM hl7.main.example_messages
ORDER BY segment_count DESC, name;
Turn a table of raw messages into an ordered message log: skip malformed rows with is_valid_hl7, read the send time (MSH-7) with hl7_get as a TIMESTAMP, and add the control id, message type, and version from the MSH accessors (here: MSG00001 ADT^A01 at 12:00, then MSG00002 ORU^R01 at 12:30).
SELECT strptime(hl7.main.hl7_get(message, 'MSH-7'), '%Y%m%d%H%M%S') AS sent_at,
hl7.main.hl7_message_control_id(message) AS control_id,
hl7.main.hl7_message_type(message) AS message_type,
hl7.main.hl7_version(message) AS version
FROM hl7.main.example_messages
WHERE hl7.main.is_valid_hl7(message)
ORDER BY sent_at, control_id;
hl7_get (message ANY, location VARCHAR)
Extract a value from an HL7 v2.x message by location (e.g. 'PID-5', 'PID-5.1', 'MSH-9.1', 'DG1[2]-3'); 1-based field/component/subcomponent indices.
Extract the patient's family name (component 1 of PID-5).
SELECT hl7.main.hl7_get(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I', 'PID-5.1');
Extract the message type field (MSH-9) by location.
SELECT hl7.main.hl7_get(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I', 'MSH-9');
Address a repeated segment: read the value (OBX-5) of the second OBX result via the 1-based repetition `OBX[2]`.
SELECT hl7.main.hl7_get(E'MSH|^~\\&|LAB|HOSP|EHR|HOSP|20240101123000||ORU^R01|MSG00002|P|2.5\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rOBR|1||ORD123|CBC^Complete Blood Count\rOBX|1|NM|WBC^White Blood Cell Count||7.2|10*3/uL|4.0-11.0|N\rOBX|2|NM|HGB^Hemoglobin||14.1|g/dL|13.5-17.5|N', 'OBX[2]-5');
1 more example
Turn a table of raw messages into an ordered message log: skip malformed rows with is_valid_hl7, read the send time (MSH-7) with hl7_get as a TIMESTAMP, and add the control id, message type, and version from the MSH accessors (here: MSG00001 ADT^A01 at 12:00, then MSG00002 ORU^R01 at 12:30).
SELECT strptime(hl7.main.hl7_get(message, 'MSH-7'), '%Y%m%d%H%M%S') AS sent_at,
hl7.main.hl7_message_control_id(message) AS control_id,
hl7.main.hl7_message_type(message) AS message_type,
hl7.main.hl7_version(message) AS version
FROM hl7.main.example_messages
WHERE hl7.main.is_valid_hl7(message)
ORDER BY sent_at, control_id;
hl7_message_control_id (message ANY)
The message control ID from MSH-10.
Read the message control ID (MSH-10) used for acknowledgements.
SELECT hl7.main.hl7_message_control_id(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I');
hl7_message_type (message ANY)
The HL7 message type from MSH-9 (e.g. 'ADT^A01' or 'ADT^A01^ADT_A01').
Read the message type (MSH-9) from an admit message.
SELECT hl7.main.hl7_message_type(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I');
hl7_version (message ANY)
The HL7 version ID from MSH-12 (e.g. '2.5').
Read the HL7 version (MSH-12) from a message.
SELECT hl7.main.hl7_version(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I');
is_valid_hl7 (message ANY)
True when the input parses as a well-formed HL7 v2.x message (begins with an MSH segment defining the field separator and encoding characters).
Check that a well-formed ADT^A01 message validates.
SELECT hl7.main.is_valid_hl7(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I');
Malformed text validates as false (never errors).
SELECT hl7.main.is_valid_hl7('not an hl7 message');
hl7_fields (message ANY)
Explode an HL7 v2.x message into long format: one row per field value (repetitions expanded), keyed by segment, segment occurrence, field, and repetition. Component structure is preserved in the raw value text.
List every field of the PID (patient identification) segment as long-format rows.
SELECT field, value
FROM hl7.main.hl7_fields(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I')
WHERE segment = 'PID';
Flatten an ORU^R01 lab report into one row per OBX result (test name, numeric value, units, reference range, abnormal flag) by pivoting hl7_fields on segment_rep, stamped with the patient MRN from hl7_get('PID-3.1') (two rows: White Blood Cell Count 7.2 and Hemoglobin 14.1 for MRN 10001).
SELECT hl7.main.hl7_get(E'MSH|^~\\&|LAB|HOSP|EHR|HOSP|20240101123000||ORU^R01|MSG00002|P|2.5\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rOBR|1||ORD123|CBC^Complete Blood Count\rOBX|1|NM|WBC^White Blood Cell Count||7.2|10*3/uL|4.0-11.0|N\rOBX|2|NM|HGB^Hemoglobin||14.1|g/dL|13.5-17.5|N', 'PID-3.1') AS mrn,
f.segment_rep + 1 AS obx_set_id,
split_part(max(f.value) FILTER (WHERE f.field = 3), '^', 2) AS test,
TRY_CAST(max(f.value) FILTER (WHERE f.field = 5) AS DOUBLE) AS result,
max(f.value) FILTER (WHERE f.field = 6) AS units,
max(f.value) FILTER (WHERE f.field = 7) AS reference_range,
max(f.value) FILTER (WHERE f.field = 8) AS abnormal_flag
FROM hl7.main.hl7_fields(E'MSH|^~\\&|LAB|HOSP|EHR|HOSP|20240101123000||ORU^R01|MSG00002|P|2.5\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rOBR|1||ORD123|CBC^Complete Blood Count\rOBX|1|NM|WBC^White Blood Cell Count||7.2|10*3/uL|4.0-11.0|N\rOBX|2|NM|HGB^Hemoglobin||14.1|g/dL|13.5-17.5|N') AS f
WHERE f.segment = 'OBX'
GROUP BY f.segment_rep
ORDER BY obx_set_id;
hl7_segments (message ANY)
Split an HL7 v2.x message into its segments: one row per segment with its 3-letter id, field count, and raw text.
List every segment of an admit message in order (MSH, EVN, PID, PV1) with its field count.
SELECT seq, segment, field_count
FROM hl7.main.hl7_segments(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I');
SELECT seq, segment, field_count
FROM hl7.main.hl7_segments(E'MSH|^~\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5
EVN|A01|20240101120000
PID|1||10001^^^HOSP^MR||Doe^John||19800101|M
PV1|1|I')
ORDER BY seq;
SELECT field, repetition, value
FROM hl7.main.hl7_fields(E'MSH|^~\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5
EVN|A01|20240101120000
PID|1||10001^^^HOSP^MR||Doe^John||19800101|M
PV1|1|I')
WHERE segment = 'PID'
ORDER BY field;
4 more examples
SELECT hl7.main.hl7_get(E'MSH|^~\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5
EVN|A01|20240101120000
PID|1||10001^^^HOSP^MR||Doe^John||19800101|M
PV1|1|I', 'PID-5.1') AS family_name;
SELECT hl7.main.hl7_message_type(E'MSH|^~\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5
EVN|A01|20240101120000
PID|1||10001^^^HOSP^MR||Doe^John||19800101|M
PV1|1|I') AS type,
hl7.main.hl7_version(E'MSH|^~\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5
EVN|A01|20240101120000
PID|1||10001^^^HOSP^MR||Doe^John||19800101|M
PV1|1|I') AS version,
hl7.main.hl7_message_control_id(E'MSH|^~\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5
EVN|A01|20240101120000
PID|1||10001^^^HOSP^MR||Doe^John||19800101|M
PV1|1|I') AS control_id;
SELECT hl7.main.is_valid_hl7(E'MSH|^~\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5
EVN|A01|20240101120000
PID|1||10001^^^HOSP^MR||Doe^John||19800101|M
PV1|1|I') AS ok;
SELECT strptime(hl7.main.hl7_get(message, 'MSH-7'), '%Y%m%d%H%M%S') AS sent_at,
hl7.main.hl7_message_control_id(message) AS control_id,
hl7.main.hl7_message_type(message) AS message_type,
hl7.main.hl7_version(message) AS version
FROM hl7.main.example_messages
WHERE hl7.main.is_valid_hl7(message)
ORDER BY sent_at, control_id;
Try these queries
Explode the PID segment into long-format field rows.
SELECT segment, field, repetition, value
FROM hl7.main.hl7_fields(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I')
WHERE segment = 'PID'
ORDER BY field, repetition
Uses hl7_fields
Extract the patient family name (component 1 of PID-5) by location.
SELECT hl7.main.hl7_get(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I', 'PID-5.1') AS family_name
Uses hl7_get
Read the message type (MSH-9) for routing a mixed feed.
SELECT hl7.main.hl7_message_type(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I') AS message_type
Uses hl7_message_type
Read the HL7 version id (MSH-12) the message claims to conform to.
SELECT hl7.main.hl7_version(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I') AS version
Uses hl7_version
Read the message control id (MSH-10) used for ACK correlation and dedupe.
SELECT hl7.main.hl7_message_control_id(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I') AS control_id
Validate that a well-formed HL7 v2 message parses.
SELECT hl7.main.is_valid_hl7(E'MSH|^~\\&|SENDING_APP|SENDING_FAC|RECV_APP|RECV_FAC|20240101120000||ADT^A01|MSG00001|P|2.5\rEVN|A01|20240101120000\rPID|1||10001^^^HOSP^MR||Doe^John||19800101|M\rPV1|1|I') AS ok
Uses is_valid_hl7
18 more examples in the Catalog, each listed under the table or function it uses.
HL7 is an independent open-source connector from Query.Farm. It is not affiliated with, endorsed by or sponsored by the services, projects or organisations it works with. Product names, logos and trademarks belong to their respective owners.
Related workers
FHIR
Science & health
Query HL7 FHIR R4 REST servers from SQL: patients, observations, generic search, reads by id, and server capabilities
Chem
Science & health
Compute molecular descriptors, fingerprints, similarity, substructure matches and InChI keys from SMILES strings
Periodic table
Science & health
The periodic table, isotopes and chemical formula molar masses as offline DuckDB SQL