Skip to content

X12 EDI

Inspect and edit healthcare and business interchanges without moving data off your machine

Connector software
Free and open source
Service credentials
Not needed
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/x12/index.md to help me query X12 EDI 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.

See what you can query

Example output · captured
Connector release
v0.6.0
Engine
2.0.1
VGI protocol
3.4.0
SELECT segment_index, element_index, value
FROM x12.main.segments_elements('ISA*00*          *00*          *ZZ*SENDER         *ZZ*RECEIVER       *240101*1200*^*00501*000000001*0*P*:~GS*XX*SEND*RECV*20240101*1200*1*X*005010~ST*837*0001~BHT*0019*00*REF01*20240101*1200*CH~CLM*ACCT777*500***11:B:1*Y~SV1*HC:99213*200*UN*1~CLM*ACCT778*1000***11:B:1*Y~SV1*HC:99214*1000*UN*1~SE*7*0001~GE*1*1~IEA*1*000000001~')
WHERE segment_id = 'CLM';
Result · first 5 rows
segment_index element_index value
2 1 ACCT777
2 2 500
2 3
2 4
2 5 11

Quick start

Choose where to run X12 EDI, 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.

setup.sql
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;

2. Attach the connector

Attach the release directly

Downloads the latest release for your platform and caches it locally.

ATTACH 'x12' AS x12 (
  TYPE vgi,
  LOCATION 'github-auto://Query-farm/vgi-x12'
);

3. Run a query

query.sql
SELECT segment_index, element_index, value
FROM x12.main.segments_elements('ISA*00*          *00*          *ZZ*SENDER         *ZZ*RECEIVER       *240101*1200*^*00501*000000001*0*P*:~GS*XX*SEND*RECV*20240101*1200*1*X*005010~ST*837*0001~BHT*0019*00*REF01*20240101*1200*CH~CLM*ACCT777*500***11:B:1*Y~SV1*HC:99213*200*UN*1~CLM*ACCT778*1000***11:B:1*Y~SV1*HC:99214*1000*UN*1~SE*7*0001~GE*1*1~IEA*1*000000001~')
WHERE segment_id = 'CLM';
More example queries →
Technical details
Verified connector release
v0.6.0
Engine
2.0.1
VGI protocol
3.4.0
Verified on
December 1, 2026
Release notes on GitHub
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 release directly

Uses the verified release each time you attach the connector.

ATTACH 'x12' AS x12 (
  TYPE vgi,
  LOCATION 'github-auto://Query-farm/[email protected]'
);

Overview

Parse ANSI ASC X12 EDI and UN/EDIFACT interchanges into queryable segment / element / envelope / shaped rows. Public syntax only; parsing is 100% local.

Catalog

Everything x12 exposes once attached — read straight from the worker's released build.

x12 1 schema
main 1 view · 16 functions

X12 / EDIFACT parsing functions: generic segment/element explode, envelope metadata, shaped healthcare/B2B views, and delimiter/type sniffers.

Schema examples (8)
Examples (8)

Pull the elements of every CLM (claim) segment from an inline 837 claim interchange (pass a file path or glob such as '/data/claims/*.837' to read your own files).

SELECT segment_index, element_index, value
FROM x12.main.segments_elements('ISA*00*          *00*          *ZZ*SENDER         *ZZ*RECEIVER       *240101*1200*^*00501*000000001*0*P*:~GS*XX*SEND*RECV*20240101*1200*1*X*005010~ST*837*0001~BHT*0019*00*REF01*20240101*1200*CH~CLM*ACCT777*500***11:B:1*Y~SV1*HC:99213*200*UN*1~CLM*ACCT778*1000***11:B:1*Y~SV1*HC:99214*1000*UN*1~SE*7*0001~GE*1*1~IEA*1*000000001~')
WHERE segment_id = 'CLM';
Result · first 5 rows
segment_index element_index value
2 1 ACCT777
2 2 500
2 3
2 4
2 5 11

Flag transactions whose SE segment count does not validate: this inline 837 declares 9 segments in SE01 but carries 4.

SELECT interchange_ctrl, transaction_type, transaction_ctrl, segment_count,
  se_count_ok
FROM x12.main.envelope('ISA*00*          *00*          *ZZ*SENDER         *ZZ*RECEIVER       *240101*1200*^*00501*000000001*0*P*:~GS*XX*SEND*RECV*20240101*1200*1*X*005010~ST*837*0001~BHT*0019*00*REF01*20240101*1200*CH~CLM*ACCT777*500***11:B:1*Y~SE*9*0001~GE*1*1~IEA*1*000000001~')
WHERE se_count_ok IS DISTINCT FROM TRUE;

Extract claim id, total paid, and payer from an inline 835 remittance interchange.

SELECT clp_claim_id, clp_total_paid, payer_name
FROM x12.main.read_835('ISA*00*          *00*          *ZZ*SENDER         *ZZ*RECEIVER       *240101*1200*^*00501*000000001*0*P*:~GS*XX*SEND*RECV*20240101*1200*1*X*005010~ST*835*0001~BPR*I*1500*C*ACH~TRN*1*TRACE0001*1512345678~NM1*PR*2*ACME HEALTH PLAN*****PI*PAYER001~NM1*PE*2*WELLNESS CLINIC LLC*****XX*1999999999~CLP*PCN1001*1*500*400*100*MC*CCN0001*11~CAS*CO*45*100~CLP*PCN1002*4*1000*0*0*MC*CCN0002*11~SE*9*0001~GE*1*1~IEA*1*000000001~');

Classify sample EDI files (committed in this repo, pinned to a release tag) by transaction set type for routing.

SELECT filename, x12.main.transaction_type(content) AS tx_type
FROM read_text(['https://raw.githubusercontent.com/Query-farm/vgi-x12/v0.5.0/data/claim_837.837', 'https://raw.githubusercontent.com/Query-farm/vgi-x12/v0.5.0/data/era_835.835', 'https://raw.githubusercontent.com/Query-farm/vgi-x12/v0.5.0/data/po_850.850', 'https://raw.githubusercontent.com/Query-farm/vgi-x12/v0.5.0/data/orders_edifact.edi'])
ORDER BY filename;
4 more examples

Count exploded EDIFACT elements per message type in an inline ORDERS interchange.

SELECT transaction_type, count(*) AS n_elements
FROM x12.main.edifact_segments('UNA:+.? ''UNB+UNOA:1+SENDER+RECEIVER+240101:1200+REF1''UNH+MSG1+ORDERS:D:96A:UN''BGM+220+PO12345+9''DTM+137:20240101:102''NAD+BY+++ACME CORP''UNT+4+MSG1''UNZ+1+REF1''')
GROUP BY transaction_type;

Route an inbox of inline payloads: sniff each one's transaction type with transaction_type and join the transaction_sets reference view to name the shaped read_* function to call (835 -> read_835, 997 -> read_997; the EDIFACT ORDERS message has no shaped view, so NULL).

SELECT inbox.filename, x12.main.transaction_type(inbox.content) AS tx_type,
  t.shaped_function
FROM (VALUES ('remit_0115.edi', 'ISA*00*          *00*          *ZZ*PAYER          *ZZ*CLINIC         *240115*0900*^*00501*000000301*0*P*:~GS*HP*PAYER*CLINIC*20240115*0900*1*X*005010X221A1~ST*835*0001~BPR*I*400*C*ACH~CLP*ACCT777*1*500*400*100*MC*CCN0001*11~SE*4*0001~GE*1*1~IEA*1*000000301~'), ('ack_0101.edi', 'ISA*00*          *00*          *ZZ*PAYER          *ZZ*CLINIC         *240101*1300*^*00501*000000302*0*P*:~GS*FA*PAYER*CLINIC*20240101*1300*1*X*005010~ST*997*0001~AK1*HC*1~AK2*837*0001~AK5*A~AK9*A*1*1*1~SE*6*0001~GE*1*1~IEA*1*000000302~'), ('orders_0102.edi', 'UNA:+.? ''UNB+UNOA:1+SENDER+RECEIVER+240102:1200+REF1''UNH+MSG1+ORDERS:D:96A:UN''BGM+220+PO12345+9''UNT+3+MSG1''UNZ+1+REF1''')) AS inbox(filename, content)
LEFT JOIN x12.main.transaction_sets AS t ON t.transaction_set = x12.main.transaction_type(inbox.content)
ORDER BY inbox.filename;

Reconcile a sent three-transaction 837 batch (envelope) against the partner's 997 acknowledgment (read_997), matched on group and transaction control numbers: 0001 has AK5 status A, 0002 has AK5 status R with one AK3 segment error, and 0003 is NOT ACKNOWLEDGED because the 997 has no AK2 for it.

WITH sent AS (SELECT group_ctrl, transaction_type, transaction_ctrl FROM x12.main.envelope('ISA*00*          *00*          *ZZ*CLINIC         *ZZ*PAYER          *240101*1200*^*00501*000000101*0*P*:~GS*HC*CLINIC*PAYER*20240101*1200*1*X*005010X222A1~ST*837*0001~BHT*0019*00*REF01*20240101*1200*CH~CLM*ACCT777*500***11:B:1*Y~SE*4*0001~ST*837*0002~BHT*0019*00*REF02*20240101*1200*CH~CLM*ACCT778*1000***11:B:1*Y~SE*4*0002~ST*837*0003~BHT*0019*00*REF03*20240101*1200*CH~CLM*ACCT779*250***11:B:1*Y~SE*4*0003~GE*3*1~IEA*1*000000101~')), acks AS (SELECT ak1_group_control, ak2_transaction_set_id, ak2_transaction_control, any_value(ak5_status) AS ak5_status, count(ak3_segment_id) AS segment_errors FROM x12.main.read_997('ISA*00*          *00*          *ZZ*PAYER          *ZZ*CLINIC         *240101*1300*^*00501*000000501*0*P*:~GS*FA*PAYER*CLINIC*20240101*1300*5*X*005010~ST*997*0001~AK1*HC*1~AK2*837*0001~AK5*A~AK2*837*0002~AK3*CLM*3**8~AK4*2*782*1~AK5*R*5~AK9*P*3*2*1~SE*10*0001~GE*1*5~IEA*1*000000501~') GROUP BY ALL) SELECT s.transaction_ctrl, coalesce(a.ak5_status, 'NOT ACKNOWLEDGED') AS ack_status, coalesce(a.segment_errors, 0) AS segment_errors
FROM sent AS s
LEFT JOIN acks AS a ON a.ak1_group_control = s.group_ctrl AND a.ak2_transaction_set_id = s.transaction_type AND a.ak2_transaction_control = s.transaction_ctrl
ORDER BY s.transaction_ctrl;

Reconcile billed claims (read_837) against the payer's remittance (read_835) by claim id: ACCT777 billed 500 and paid 400 (CLP02 status 1, 100 unpaid), ACCT778 billed 1000 and paid 0 (CLP02 status 4), ACCT779 billed 250 with no remittance yet (NULL status and paid).

WITH billed AS (SELECT DISTINCT clm_id, CAST(clm_total_charge AS DECIMAL(12, 2)) AS billed FROM x12.main.read_837('ISA*00*          *00*          *ZZ*CLINIC         *ZZ*PAYER          *240101*1200*^*00501*000000201*0*P*:~GS*HC*CLINIC*PAYER*20240101*1200*1*X*005010X222A1~ST*837*0001~BHT*0019*00*REF01*20240101*1200*CH~NM1*85*2*BILLING CLINIC*****XX*1122334455~NM1*IL*1*DOE*JOHN****MI*MEMBER123~CLM*ACCT777*500***11:B:1*Y~SV1*HC:99213*200*UN*1~SV1*HC:85025*300*UN*1~CLM*ACCT778*1000***11:B:1*Y~SV1*HC:99214*1000*UN*1~CLM*ACCT779*250***11:B:1*Y~SV1*HC:99212*250*UN*1~SE*12*0001~GE*1*1~IEA*1*000000201~')), paid AS (SELECT DISTINCT clp_claim_id, clp_status_code, CAST(clp_total_paid AS DECIMAL(12, 2)) AS paid FROM x12.main.read_835('ISA*00*          *00*          *ZZ*PAYER          *ZZ*CLINIC         *240115*0900*^*00501*000000301*0*P*:~GS*HP*PAYER*CLINIC*20240115*0900*1*X*005010X221A1~ST*835*0001~BPR*I*400*C*ACH~CLP*ACCT777*1*500*400*100*MC*CCN0001*11~CAS*PR*1*100~CLP*ACCT778*4*1000*0*0*MC*CCN0002*11~CAS*CO*50*1000~SE*7*0001~GE*1*1~IEA*1*000000301~')) SELECT b.clm_id, b.billed, p.clp_status_code, p.paid, b.billed - coalesce(p.paid, 0) AS unpaid
FROM billed AS b
LEFT JOIN paid AS p ON p.clp_claim_id = b.clm_id
ORDER BY b.clm_id;
Views
transaction_sets 3 examples4 cols

Reference view mapping each shaped X12 transaction set to its read_* function.

Columns
transaction_set VARCHAR
X12 ST01 transaction set identifier (e.g. '835').
family VARCHAR
EDI family — 'X12' for these shaped views.
shaped_function VARCHAR
The x12.main read_* table function that projects a relational view of this set.
summary VARCHAR
Short general-industry description of the set (no copyrighted TR3 text).
Examples (3)

List the shaped X12 transaction sets and their read_* function.

SELECT transaction_set, shaped_function
FROM x12.main.transaction_sets
WHERE family = 'X12'
ORDER BY transaction_set;

Find which shaped function parses 835 remittance advice.

SELECT shaped_function, summary
FROM x12.main.transaction_sets
WHERE transaction_set = '835';

List every shaped transaction set and its read_* function.

SELECT transaction_set, shaped_function
FROM x12.main.transaction_sets
ORDER BY transaction_set;
Link
Scalar functions
delimiters (content VARCHAR) 1 example

Sniff the X12/EDIFACT delimiters from inline content into a STRUCT(element, segment, component, repetition)

Arguments
content VARCHAR
Inline interchange content whose delimiters to sniff — e.g. the first bytes of an ISA or UNA/UNB header. Typically the content column from read_text(...). NULL or unrecognized content yields a NULL struct.
ReturnsSTRUCT of 4 fields
STRUCT 4 fields
  • elementVARCHAR
  • segmentVARCHAR
  • componentVARCHAR
  • repetitionVARCHAR
Example

Sniff the element separator (ISA byte-3) from a canonical ISA header.

SELECT (x12.main.delimiters('ISA*00*          *00*          *ZZ*SENDER         *ZZ*RECEIVER       *240101*1200*^*00501*000000001*0*P*:~')).element AS element_sep;
Link
transaction_type (content VARCHAR) 2 examples

Detect the transaction type (first ST01, or EDIFACT UNH02 message type) of inline content

Arguments
content VARCHAR
Inline interchange content. Returns the first X12 ST01 transaction set identifier or the EDIFACT UNH02 message type; NULL when none is found. Typically the content column from read_text(...).
ReturnsVARCHAR
Examples (2)

Detect that an inline interchange carries an 835.

SELECT x12.main.transaction_type('ISA*00*          *00*          *ZZ*SENDER         *ZZ*RECEIVER       *240101*1200*^*00501*1*0*P*:~GS*HP*S*R*20240101*1200*1*X*005010X221A1~ST*835*0001~SE*1*0001~GE*1*1~IEA*1*1~') AS tx_type;

Route an inbox of inline payloads: sniff each one's transaction type with transaction_type and join the transaction_sets reference view to name the shaped read_* function to call (835 -> read_835, 997 -> read_997; the EDIFACT ORDERS message has no shaped view, so NULL).

SELECT inbox.filename, x12.main.transaction_type(inbox.content) AS tx_type,
  t.shaped_function
FROM (VALUES ('remit_0115.edi', 'ISA*00*          *00*          *ZZ*PAYER          *ZZ*CLINIC         *240115*0900*^*00501*000000301*0*P*:~GS*HP*PAYER*CLINIC*20240115*0900*1*X*005010X221A1~ST*835*0001~BPR*I*400*C*ACH~CLP*ACCT777*1*500*400*100*MC*CCN0001*11~SE*4*0001~GE*1*1~IEA*1*000000301~'), ('ack_0101.edi', 'ISA*00*          *00*          *ZZ*PAYER          *ZZ*CLINIC         *240101*1300*^*00501*000000302*0*P*:~GS*FA*PAYER*CLINIC*20240101*1300*1*X*005010~ST*997*0001~AK1*HC*1~AK2*837*0001~AK5*A~AK9*A*1*1*1~SE*6*0001~GE*1*1~IEA*1*000000302~'), ('orders_0102.edi', 'UNA:+.? ''UNB+UNOA:1+SENDER+RECEIVER+240102:1200+REF1''UNH+MSG1+ORDERS:D:96A:UN''BGM+220+PO12345+9''UNT+3+MSG1''UNZ+1+REF1''')) AS inbox(filename, content)
LEFT JOIN x12.main.transaction_sets AS t ON t.transaction_set = x12.main.transaction_type(inbox.content)
ORDER BY inbox.filename;
Link
Table functions
copy_x12 (recalculate_counts := BOOLEAN) +1 overload 1 example

Import and export complete X12 segment rows through the catalog's COPY format.

Named arguments
recalculate_counts := BOOLEAN
Recompute SE01, GE01, and IEA01 after edits. Defaults to false (validate existing counts). Never changes control numbers or synthesizes missing envelopes.
Overloads
copy_x12()copy_x12(recalculate_counts BOOLEAN)
Returns columns
interchange_ctrl VARCHAR
ISA13 — interchange control number of the row's interchange.
group_ctrl VARCHAR
GS06 — functional group control number of the row's group.
transaction_ctrl VARCHAR
ST02 — transaction set control number of the row's transaction.
transaction_type VARCHAR
ST01 — transaction set identifier ('835','837',…); UNH02 message type for EDIFACT.
source_path VARCHAR
The file the row came from; NULL for inline content.
document_index BIGINT
Zero-based document ordinal within this invocation; distinguishes inline and repeated input paths.
segment_index BIGINT
Zero-based segment ordinal across the whole document, including all envelopes. Order by document_index, segment_index before COPY TO.
segment_id VARCHAR
Raw segment identifier, including ISA, GS, ST, SE, GE, and IEA.
elements VARCHAR[]
All positional element values, preserving empty values, padding, composites, and repetitions as unsplit text.
byte_offset BIGINT
Start byte offset in the source document; informational after edits.
element_separator VARCHAR
Element separator in force for this row's interchange.
segment_terminator VARCHAR
Segment terminator in force for this row's interchange. Component and repetition separators remain in ISA16 and ISA11.
Example

Export an X12 interchange's segment rows to a file through the X12 COPY format, in document and segment order.

COPY (SELECT * FROM x12.main.read_x12('ISA*00*          *00*          *ZZ*SENDER         *ZZ*RECEIVER       *240101*1200*^*00501*000000001*0*P*:~GS*XX*SEND*RECV*20240101*1200*1*X*005010~ST*997*0001~AK1*PO*1~AK9*A*1*1*1~SE*4*0001~GE*1*1~IEA*1*000000001~') ORDER BY document_index, segment_index) TO 'acknowledgment.x12' (FORMAT 'x12.x12');
Link
edifact_envelope (input ANY, mode := VARCHAR) 1 example

One row per UN/EDIFACT message: UNB/UNG/UNH envelope metadata + UNT validity

Positional arguments
input ANY
The EDI to parse, overloaded across three auto-detected input modes: a file path or glob (e.g. '/data/claims/*.837'), or a list of paths — the streaming hot path, read locally in sorted order; OR an inline interchange string; OR inline interchange bytes. Inline content is recognized by its ISA / UNA / UNB magic prefix; anything else is treated as a file path. Inline content has a NULL source_path. Parsing is 100% local — no outbound calls. Use `mode =>` to force path versus content.
Named arguments
mode := VARCHAR
Force how `input` is interpreted: 'path' (read file(s)/glob) or 'content' (parse the value inline). Omit to auto-detect from the ISA/UNA/UNB magic prefix.
Returns columns
interchange_ctrl VARCHAR
UNB05 — interchange control reference.
syntax_id VARCHAR
UNB01-1 — syntax identifier (e.g. 'UNOA').
syntax_version VARCHAR
UNB01-2 — syntax version number.
sender_id VARCHAR
UNB02-1 — interchange sender identification.
receiver_id VARCHAR
UNB03-1 — interchange recipient identification.
unb_date VARCHAR
UNB04-1 — preparation date (YYMMDD).
unb_time VARCHAR
UNB04-2 — preparation time (HHMM).
group_ctrl VARCHAR
UNG05 — functional group reference (NULL when no UNG).
transaction_ctrl VARCHAR
UNH01 — message reference number.
transaction_type VARCHAR
UNH02-1 — message type ('ORDERS','INVOIC',…).
message_version VARCHAR
UNH02-2 — message version number.
message_release VARCHAR
UNH02-3 — message release number.
controlling_agency VARCHAR
UNH02-4 — controlling agency (e.g. 'UN').
segment_count BIGINT
Actual UNH..UNT segment count (what UNT01 must equal).
unt_count_ok BOOLEAN
Whether UNT01 equals the actual segment count; NULL if no UNT.
source_path VARCHAR
The file the row came from; NULL for inline content.
Example

Summarize an inline EDIFACT ORDERS interchange as one envelope row.

SELECT interchange_ctrl, syntax_id, sender_id, receiver_id
FROM x12.main.edifact_envelope('UNA:+.? ''UNB+UNOA:1+SENDER+RECEIVER+240101:1200+REF1''UNH+MSG1+ORDERS:D:96A:UN''BGM+220+PO12345+9''DTM+137:20240101:102''NAD+BY+++ACME CORP''UNT+4+MSG1''UNZ+1+REF1''');
Link
edifact_segments (input ANY, mode := VARCHAR) 1 example

Explode a UN/EDIFACT interchange into one row per element

Positional arguments
input ANY
The EDI to parse, overloaded across three auto-detected input modes: a file path or glob (e.g. '/data/claims/*.837'), or a list of paths — the streaming hot path, read locally in sorted order; OR an inline interchange string; OR inline interchange bytes. Inline content is recognized by its ISA / UNA / UNB magic prefix; anything else is treated as a file path. Inline content has a NULL source_path. Parsing is 100% local — no outbound calls. Use `mode =>` to force path versus content.
Named arguments
mode := VARCHAR
Force how `input` is interpreted: 'path' (read file(s)/glob) or 'content' (parse the value inline). Omit to auto-detect from the ISA/UNA/UNB magic prefix.
Returns columns
interchange_ctrl VARCHAR
ISA13 — interchange control number of the row's interchange.
group_ctrl VARCHAR
GS06 — functional group control number of the row's group.
transaction_ctrl VARCHAR
ST02 — transaction set control number of the row's transaction.
transaction_type VARCHAR
ST01 — transaction set identifier ('835','837',…); UNH02 message type for EDIFACT.
source_path VARCHAR
The file the row came from; NULL for inline content.
segment_index BIGINT
0-based ordinal of this element's segment within its UNH..UNT message.
segment_id VARCHAR
Raw EDIFACT segment tag (e.g. 'BGM', 'DTM', 'NAD').
element_index INTEGER
1-based data element position within the segment.
component_index INTEGER
1-based component (sub-element) position; NULL unless the element is composite.
value VARCHAR
The raw element/component value, with the EDIFACT release char applied.
Example

Explode an inline EDIFACT ORDERS interchange into one row per element.

SELECT segment_id, element_index, value
FROM x12.main.edifact_segments('UNA:+.? ''UNB+UNOA:1+SENDER+RECEIVER+240101:1200+REF1''UNH+MSG1+ORDERS:D:96A:UN''BGM+220+PO12345+9''DTM+137:20240101:102''NAD+BY+++ACME CORP''UNT+4+MSG1''UNZ+1+REF1''');
Link
envelope (input ANY, mode := VARCHAR) 2 examples

One row per ST transaction: ISA/GS/ST envelope metadata + structural validity flags

Positional arguments
input ANY
The EDI to parse, overloaded across three auto-detected input modes: a file path or glob (e.g. '/data/claims/*.837'), or a list of paths — the streaming hot path, read locally in sorted order; OR an inline interchange string; OR inline interchange bytes. Inline content is recognized by its ISA / UNA / UNB magic prefix; anything else is treated as a file path. Inline content has a NULL source_path. Parsing is 100% local — no outbound calls. Use `mode =>` to force path versus content.
Named arguments
mode := VARCHAR
Force how `input` is interpreted: 'path' (read file(s)/glob) or 'content' (parse the value inline). Omit to auto-detect from the ISA/UNA/UNB magic prefix.
Returns columns
interchange_ctrl VARCHAR
ISA13 — interchange control number.
sender_qual VARCHAR
ISA05 — interchange sender ID qualifier.
sender_id VARCHAR
ISA06 — interchange sender ID (15-char, raw).
receiver_qual VARCHAR
ISA07 — interchange receiver ID qualifier.
receiver_id VARCHAR
ISA08 — interchange receiver ID (raw).
isa_date VARCHAR
ISA09 — interchange date (YYMMDD).
isa_time VARCHAR
ISA10 — interchange time (HHMM).
isa_version VARCHAR
ISA12 — interchange control version number (e.g. '00501').
usage_indicator VARCHAR
ISA15 — usage indicator ('P' production, 'T' test).
group_ctrl VARCHAR
GS06 — functional group control number.
gs_functional_id VARCHAR
GS01 — functional identifier code (e.g. HP/HC/HS/HB/PO/FA).
gs_app_sender VARCHAR
GS02 — application sender code.
gs_app_receiver VARCHAR
GS03 — application receiver code.
gs_date VARCHAR
GS04 — functional group date (CCYYMMDD).
gs_version VARCHAR
GS08 — version / release / industry identifier (raw string).
transaction_ctrl VARCHAR
ST02 — transaction set control number.
transaction_type VARCHAR
ST01 — transaction set identifier ('835','837',…).
st_impl_ref VARCHAR
ST03 — implementation convention reference (optional).
segment_count BIGINT
Actual ST..SE segment count (what SE01 must equal).
se_count_ok BOOLEAN
Whether SE01 equals the actual segment count; NULL if no SE.
ge_count_ok BOOLEAN
Whether GE01 equals the group's transaction count; NULL if no GE.
iea_count_ok BOOLEAN
Whether IEA01 equals the interchange's group count; NULL if no IEA.
se_ctrl_match BOOLEAN
Whether SE02 matches ST02; NULL if no SE.
ge_ctrl_match BOOLEAN
Whether GE02 matches GS06; NULL if no GE.
iea_ctrl_match BOOLEAN
Whether IEA02 matches ISA13; NULL if no IEA.
source_path VARCHAR
The file the row came from; NULL for inline content.
Examples (2)

Summarize an inline 837 interchange as one envelope row with structural validity flags.

SELECT interchange_ctrl, transaction_type, segment_count, se_count_ok
FROM x12.main.envelope('ISA*00*          *00*          *ZZ*SENDER         *ZZ*RECEIVER       *240101*1200*^*00501*000000001*0*P*:~GS*XX*SEND*RECV*20240101*1200*1*X*005010~ST*837*0001~BHT*0019*00*REF01*20240101*1200*CH~CLM*ACCT777*500***11:B:1*Y~SV1*HC:99213*200*UN*1~SE*5*0001~GE*1*1~IEA*1*000000001~');

Reconcile a sent three-transaction 837 batch (envelope) against the partner's 997 acknowledgment (read_997), matched on group and transaction control numbers: 0001 has AK5 status A, 0002 has AK5 status R with one AK3 segment error, and 0003 is NOT ACKNOWLEDGED because the 997 has no AK2 for it.

WITH sent AS (SELECT group_ctrl, transaction_type, transaction_ctrl FROM x12.main.envelope('ISA*00*          *00*          *ZZ*CLINIC         *ZZ*PAYER          *240101*1200*^*00501*000000101*0*P*:~GS*HC*CLINIC*PAYER*20240101*1200*1*X*005010X222A1~ST*837*0001~BHT*0019*00*REF01*20240101*1200*CH~CLM*ACCT777*500***11:B:1*Y~SE*4*0001~ST*837*0002~BHT*0019*00*REF02*20240101*1200*CH~CLM*ACCT778*1000***11:B:1*Y~SE*4*0002~ST*837*0003~BHT*0019*00*REF03*20240101*1200*CH~CLM*ACCT779*250***11:B:1*Y~SE*4*0003~GE*3*1~IEA*1*000000101~')), acks AS (SELECT ak1_group_control, ak2_transaction_set_id, ak2_transaction_control, any_value(ak5_status) AS ak5_status, count(ak3_segment_id) AS segment_errors FROM x12.main.read_997('ISA*00*          *00*          *ZZ*PAYER          *ZZ*CLINIC         *240101*1300*^*00501*000000501*0*P*:~GS*FA*PAYER*CLINIC*20240101*1300*5*X*005010~ST*997*0001~AK1*HC*1~AK2*837*0001~AK5*A~AK2*837*0002~AK3*CLM*3**8~AK4*2*782*1~AK5*R*5~AK9*P*3*2*1~SE*10*0001~GE*1*5~IEA*1*000000501~') GROUP BY ALL) SELECT s.transaction_ctrl, coalesce(a.ak5_status, 'NOT ACKNOWLEDGED') AS ack_status, coalesce(a.segment_errors, 0) AS segment_errors
FROM sent AS s
LEFT JOIN acks AS a ON a.ak1_group_control = s.group_ctrl AND a.ak2_transaction_set_id = s.transaction_type AND a.ak2_transaction_control = s.transaction_ctrl
ORDER BY s.transaction_ctrl;
Link
read_270 (input ANY, mode := VARCHAR) 1 example

Shaped positional view of X12 transaction set 270

Positional arguments
input ANY
The EDI to parse, overloaded across three auto-detected input modes: a file path or glob (e.g. '/data/claims/*.837'), or a list of paths — the streaming hot path, read locally in sorted order; OR an inline interchange string; OR inline interchange bytes. Inline content is recognized by its ISA / UNA / UNB magic prefix; anything else is treated as a file path. Inline content has a NULL source_path. Parsing is 100% local — no outbound calls. Use `mode =>` to force path versus content.
Named arguments
mode := VARCHAR
Force how `input` is interpreted: 'path' (read file(s)/glob) or 'content' (parse the value inline). Omit to auto-detect from the ISA/UNA/UNB magic prefix.
Returns columns
interchange_ctrl VARCHAR
ISA13 — interchange control number of the row's interchange.
group_ctrl VARCHAR
GS06 — functional group control number of the row's group.
transaction_ctrl VARCHAR
ST02 — transaction set control number of the row's transaction.
transaction_type VARCHAR
ST01 — transaction set identifier ('835','837',…); UNH02 message type for EDIFACT.
source_path VARCHAR
The file the row came from; NULL for inline content.
hl_id VARCHAR
HL01 — hierarchical ID number.
hl_parent VARCHAR
HL02 — hierarchical parent ID number.
hl_level_code VARCHAR
HL03 — hierarchical level code (raw, e.g. 20/21/22/23).
hl_child_code VARCHAR
HL04 — hierarchical child code (raw).
entity_qualifier VARCHAR
NM101 of the HL's NM1 — entity identifier code (raw).
entity_name VARCHAR
NM103 of the HL's NM1 — entity name (raw).
entity_id_qualifier VARCHAR
NM108 of the HL's NM1 — identification code qualifier (raw).
entity_id VARCHAR
NM109 of the HL's NM1 — entity identification code.
trn_trace VARCHAR
TRN02 — trace number under the HL.
eb_eligibility_code VARCHAR
EB01 — eligibility/benefit information code (271, raw).
eb_coverage_level VARCHAR
EB02 — coverage level code (271, raw).
eb_service_type VARCHAR
EB03 — service type code (271, raw).
eb_insurance_type VARCHAR
EB04 — insurance type code (271, raw).
eb_plan_description VARCHAR
EB05 — plan coverage description (271, raw).
eb_time_qualifier VARCHAR
EB06 — time period qualifier (271, raw).
eb_benefit_amount VARCHAR
EB07 — benefit amount (271, raw VARCHAR).
dtp_qualifier VARCHAR
DTP01 — date/time qualifier (raw).
dtp_date VARCHAR
DTP03 — date value (raw).
aaa_reject_reason VARCHAR
AAA03 — request validation / reject reason code (raw).
Example

Project an inline 270 interchange through the read_270 shaped view.

SELECT hl_id, hl_level_code, entity_name
FROM x12.main.read_270('ISA*00*          *00*          *ZZ*SENDER         *ZZ*RECEIVER       *240101*1200*^*00501*000000001*0*P*:~GS*XX*SEND*RECV*20240101*1200*1*X*005010~ST*270*0001~HL*1**20*1~NM1*PR*2*ACME PAYER*****PI*PAYER01~HL*2*1*21*1~NM1*1P*2*PROVIDER GRP*****XX*1444444444~HL*3*2*22*0~NM1*IL*1*DOE*JOHN****MI*MEMBER99~TRN*1*TRACE70*9999999999~EQ*30~SE*10*0001~GE*1*1~IEA*1*000000001~');
Link
read_271 (input ANY, mode := VARCHAR) 1 example

Shaped positional view of X12 transaction set 271

Positional arguments
input ANY
The EDI to parse, overloaded across three auto-detected input modes: a file path or glob (e.g. '/data/claims/*.837'), or a list of paths — the streaming hot path, read locally in sorted order; OR an inline interchange string; OR inline interchange bytes. Inline content is recognized by its ISA / UNA / UNB magic prefix; anything else is treated as a file path. Inline content has a NULL source_path. Parsing is 100% local — no outbound calls. Use `mode =>` to force path versus content.
Named arguments
mode := VARCHAR
Force how `input` is interpreted: 'path' (read file(s)/glob) or 'content' (parse the value inline). Omit to auto-detect from the ISA/UNA/UNB magic prefix.
Returns columns
interchange_ctrl VARCHAR
ISA13 — interchange control number of the row's interchange.
group_ctrl VARCHAR
GS06 — functional group control number of the row's group.
transaction_ctrl VARCHAR
ST02 — transaction set control number of the row's transaction.
transaction_type VARCHAR
ST01 — transaction set identifier ('835','837',…); UNH02 message type for EDIFACT.
source_path VARCHAR
The file the row came from; NULL for inline content.
hl_id VARCHAR
HL01 — hierarchical ID number.
hl_parent VARCHAR
HL02 — hierarchical parent ID number.
hl_level_code VARCHAR
HL03 — hierarchical level code (raw, e.g. 20/21/22/23).
hl_child_code VARCHAR
HL04 — hierarchical child code (raw).
entity_qualifier VARCHAR
NM101 of the HL's NM1 — entity identifier code (raw).
entity_name VARCHAR
NM103 of the HL's NM1 — entity name (raw).
entity_id_qualifier VARCHAR
NM108 of the HL's NM1 — identification code qualifier (raw).
entity_id VARCHAR
NM109 of the HL's NM1 — entity identification code.
trn_trace VARCHAR
TRN02 — trace number under the HL.
eb_eligibility_code VARCHAR
EB01 — eligibility/benefit information code (271, raw).
eb_coverage_level VARCHAR
EB02 — coverage level code (271, raw).
eb_service_type VARCHAR
EB03 — service type code (271, raw).
eb_insurance_type VARCHAR
EB04 — insurance type code (271, raw).
eb_plan_description VARCHAR
EB05 — plan coverage description (271, raw).
eb_time_qualifier VARCHAR
EB06 — time period qualifier (271, raw).
eb_benefit_amount VARCHAR
EB07 — benefit amount (271, raw VARCHAR).
dtp_qualifier VARCHAR
DTP01 — date/time qualifier (raw).
dtp_date VARCHAR
DTP03 — date value (raw).
aaa_reject_reason VARCHAR
AAA03 — request validation / reject reason code (raw).
Example

Project an inline 271 interchange through the read_271 shaped view.

SELECT hl_id, entity_name, eb_plan_description
FROM x12.main.read_271('ISA*00*          *00*          *ZZ*SENDER         *ZZ*RECEIVER       *240101*1200*^*00501*000000001*0*P*:~GS*XX*SEND*RECV*20240101*1200*1*X*005010~ST*271*0001~HL*1**20*1~NM1*PR*2*ACME PAYER*****PI*PAYER01~HL*2*1*21*1~NM1*1P*2*PROVIDER GRP*****XX*1444444444~HL*3*2*22*0~NM1*IL*1*DOE*JOHN****MI*MEMBER99~TRN*2*TRACE55*9999999999~EB*1*IND*30**GOLD PPO~EB*B*IND*30****27.5~DTP*291*D8*20240101~SE*12*0001~GE*1*1~IEA*1*000000001~');
Link
read_835 (input ANY, mode := VARCHAR) 1 example

Shaped positional view of X12 transaction set 835

Positional arguments
input ANY
The EDI to parse, overloaded across three auto-detected input modes: a file path or glob (e.g. '/data/claims/*.837'), or a list of paths — the streaming hot path, read locally in sorted order; OR an inline interchange string; OR inline interchange bytes. Inline content is recognized by its ISA / UNA / UNB magic prefix; anything else is treated as a file path. Inline content has a NULL source_path. Parsing is 100% local — no outbound calls. Use `mode =>` to force path versus content.
Named arguments
mode := VARCHAR
Force how `input` is interpreted: 'path' (read file(s)/glob) or 'content' (parse the value inline). Omit to auto-detect from the ISA/UNA/UNB magic prefix.
Returns columns
interchange_ctrl VARCHAR
ISA13 — interchange control number of the row's interchange.
group_ctrl VARCHAR
GS06 — functional group control number of the row's group.
transaction_ctrl VARCHAR
ST02 — transaction set control number of the row's transaction.
transaction_type VARCHAR
ST01 — transaction set identifier ('835','837',…); UNH02 message type for EDIFACT.
source_path VARCHAR
The file the row came from; NULL for inline content.
bpr_total_paid VARCHAR
BPR02 — total actual provider payment amount (raw VARCHAR; cast as needed).
bpr_credit_debit VARCHAR
BPR03 — credit/debit flag (raw code).
bpr_payment_method VARCHAR
BPR04 — payment method code (raw, e.g. ACH/CHK).
bpr_payment_date VARCHAR
BPR16 — payment effective date (raw CCYYMMDD).
trn_trace_number VARCHAR
TRN02 — reassociation trace number.
trn_payer_id VARCHAR
TRN03 — originating company / payer identifier.
payer_name VARCHAR
NM103 of the NM1*PR loop — payer name (raw).
payee_name VARCHAR
NM103 of the NM1*PE loop — payee name (raw).
payee_npi VARCHAR
NM109 of the NM1*PE loop — payee identifier (e.g. NPI).
clp_claim_id VARCHAR
CLP01 — patient control number (claim id).
clp_status_code VARCHAR
CLP02 — claim status code (raw).
clp_total_charge VARCHAR
CLP03 — total submitted charge (raw VARCHAR).
clp_total_paid VARCHAR
CLP04 — total paid amount (raw VARCHAR).
clp_patient_resp VARCHAR
CLP05 — patient responsibility amount (raw VARCHAR).
clp_filing_code VARCHAR
CLP06 — claim filing indicator code (raw).
clp_payer_claim_control VARCHAR
CLP07 — payer claim control number.
clp_facility_code VARCHAR
CLP08 — facility type / place-of-service code (raw).
cas_group_code VARCHAR
CAS01 — claim-adjustment group code (raw).
cas_reason_code VARCHAR
CAS02 — claim-adjustment reason code (raw).
cas_amount VARCHAR
CAS03 — adjustment amount (raw VARCHAR).
svc_procedure VARCHAR
SVC01 — service procedure composite (raw, component-joined).
svc_charge VARCHAR
SVC02 — line item charge amount (raw VARCHAR).
svc_paid VARCHAR
SVC03 — line item paid amount (raw VARCHAR).
svc_units VARCHAR
SVC05 — units of service (raw VARCHAR).
ref_qualifier VARCHAR
REF01 — reference identification qualifier (raw).
ref_value VARCHAR
REF02 — reference identification value.
dtm_qualifier VARCHAR
DTM01 — date/time qualifier (raw).
dtm_date VARCHAR
DTM02 — date value (raw CCYYMMDD).
Example

Project an inline 835 interchange through the read_835 shaped view.

SELECT clp_claim_id, clp_total_paid, payer_name
FROM x12.main.read_835('ISA*00*          *00*          *ZZ*SENDER         *ZZ*RECEIVER       *240101*1200*^*00501*000000001*0*P*:~GS*XX*SEND*RECV*20240101*1200*1*X*005010~ST*835*0001~BPR*I*1500*C*ACH~TRN*1*TRACE0001*1512345678~NM1*PR*2*ACME HEALTH PLAN*****PI*PAYER001~NM1*PE*2*WELLNESS CLINIC LLC*****XX*1999999999~CLP*PCN1001*1*500*400*100*MC*CCN0001*11~CAS*CO*45*100~SVC*HC:99213*200*160**1~DTM*232*20240110~CLP*PCN1002*4*1000*0*0*MC*CCN0002*11~SE*11*0001~GE*1*1~IEA*1*000000001~');
Link
read_837 (input ANY, mode := VARCHAR) 2 examples

Shaped positional view of X12 transaction set 837

Positional arguments
input ANY
The EDI to parse, overloaded across three auto-detected input modes: a file path or glob (e.g. '/data/claims/*.837'), or a list of paths — the streaming hot path, read locally in sorted order; OR an inline interchange string; OR inline interchange bytes. Inline content is recognized by its ISA / UNA / UNB magic prefix; anything else is treated as a file path. Inline content has a NULL source_path. Parsing is 100% local — no outbound calls. Use `mode =>` to force path versus content.
Named arguments
mode := VARCHAR
Force how `input` is interpreted: 'path' (read file(s)/glob) or 'content' (parse the value inline). Omit to auto-detect from the ISA/UNA/UNB magic prefix.
Returns columns
interchange_ctrl VARCHAR
ISA13 — interchange control number of the row's interchange.
group_ctrl VARCHAR
GS06 — functional group control number of the row's group.
transaction_ctrl VARCHAR
ST02 — transaction set control number of the row's transaction.
transaction_type VARCHAR
ST01 — transaction set identifier ('835','837',…); UNH02 message type for EDIFACT.
source_path VARCHAR
The file the row came from; NULL for inline content.
billing_provider_name VARCHAR
NM103 of the NM1*85 loop — billing provider name (raw).
billing_provider_npi VARCHAR
NM109 of the NM1*85 loop — billing provider identifier (NPI).
subscriber_name VARCHAR
NM103 of the NM1*IL loop — subscriber name (raw).
subscriber_id VARCHAR
NM109 of the NM1*IL loop — subscriber primary identifier.
patient_name VARCHAR
NM103 of the NM1*QC loop — patient name (raw).
sbr_payer_responsibility VARCHAR
SBR01 — payer responsibility sequence code (raw).
sbr_relationship VARCHAR
SBR02 — individual relationship code (raw).
sbr_plan_name VARCHAR
SBR04 — insured group / plan name (raw).
clm_id VARCHAR
CLM01 — patient account / claim submitter identifier.
clm_total_charge VARCHAR
CLM02 — total claim charge amount (raw VARCHAR).
clm_place_of_service VARCHAR
CLM05 — health-care service location composite (raw, component-joined).
clm_provider_signature VARCHAR
CLM06 — provider-signature-on-file indicator (raw).
hi_diagnosis_qualifier VARCHAR
HI01-1 — diagnosis code-list qualifier (raw).
hi_diagnosis_code VARCHAR
HI01-2 — diagnosis code (raw).
sv1_procedure VARCHAR
SV101 — professional service procedure composite (raw, component-joined).
sv1_charge VARCHAR
SV102 — professional line item charge (raw VARCHAR).
sv1_units VARCHAR
SV104 — professional service unit count (raw VARCHAR).
sv2_revenue_code VARCHAR
SV201 — institutional revenue code (raw).
sv2_procedure VARCHAR
SV202 — institutional procedure composite (raw, component-joined).
sv2_charge VARCHAR
SV203 — institutional line item charge (raw VARCHAR).
dtp_qualifier VARCHAR
DTP01 — date/time qualifier (raw).
dtp_date VARCHAR
DTP03 — date value (raw).
Examples (2)

Project an inline 837 interchange through the read_837 shaped view.

SELECT billing_provider_npi, subscriber_id, clm_place_of_service
FROM x12.main.read_837('ISA*00*          *00*          *ZZ*SENDER         *ZZ*RECEIVER       *240101*1200*^*00501*000000001*0*P*:~GS*XX*SEND*RECV*20240101*1200*1*X*005010~ST*837*0001~BHT*0019*00*REF01*20240101*1200*CH~NM1*85*2*BILLING CLINIC*****XX*1122334455~NM1*IL*1*DOE*JOHN****MI*MEMBER123~SBR*P*18**GROUP PLAN~NM1*QC*1*DOE*JANE~CLM*ACCT777*500***11:B:1*Y~HI*ABK:Z1234~SV1*HC:99213*200*UN*1~DTP*472*D8*20240105~SE*11*0001~GE*1*1~IEA*1*000000001~');

Reconcile billed claims (read_837) against the payer's remittance (read_835) by claim id: ACCT777 billed 500 and paid 400 (CLP02 status 1, 100 unpaid), ACCT778 billed 1000 and paid 0 (CLP02 status 4), ACCT779 billed 250 with no remittance yet (NULL status and paid).

WITH billed AS (SELECT DISTINCT clm_id, CAST(clm_total_charge AS DECIMAL(12, 2)) AS billed FROM x12.main.read_837('ISA*00*          *00*          *ZZ*CLINIC         *ZZ*PAYER          *240101*1200*^*00501*000000201*0*P*:~GS*HC*CLINIC*PAYER*20240101*1200*1*X*005010X222A1~ST*837*0001~BHT*0019*00*REF01*20240101*1200*CH~NM1*85*2*BILLING CLINIC*****XX*1122334455~NM1*IL*1*DOE*JOHN****MI*MEMBER123~CLM*ACCT777*500***11:B:1*Y~SV1*HC:99213*200*UN*1~SV1*HC:85025*300*UN*1~CLM*ACCT778*1000***11:B:1*Y~SV1*HC:99214*1000*UN*1~CLM*ACCT779*250***11:B:1*Y~SV1*HC:99212*250*UN*1~SE*12*0001~GE*1*1~IEA*1*000000201~')), paid AS (SELECT DISTINCT clp_claim_id, clp_status_code, CAST(clp_total_paid AS DECIMAL(12, 2)) AS paid FROM x12.main.read_835('ISA*00*          *00*          *ZZ*PAYER          *ZZ*CLINIC         *240115*0900*^*00501*000000301*0*P*:~GS*HP*PAYER*CLINIC*20240115*0900*1*X*005010X221A1~ST*835*0001~BPR*I*400*C*ACH~CLP*ACCT777*1*500*400*100*MC*CCN0001*11~CAS*PR*1*100~CLP*ACCT778*4*1000*0*0*MC*CCN0002*11~CAS*CO*50*1000~SE*7*0001~GE*1*1~IEA*1*000000301~')) SELECT b.clm_id, b.billed, p.clp_status_code, p.paid, b.billed - coalesce(p.paid, 0) AS unpaid
FROM billed AS b
LEFT JOIN paid AS p ON p.clp_claim_id = b.clm_id
ORDER BY b.clm_id;
Link
read_850 (input ANY, mode := VARCHAR) 1 example

Shaped positional view of X12 transaction set 850

Positional arguments
input ANY
The EDI to parse, overloaded across three auto-detected input modes: a file path or glob (e.g. '/data/claims/*.837'), or a list of paths — the streaming hot path, read locally in sorted order; OR an inline interchange string; OR inline interchange bytes. Inline content is recognized by its ISA / UNA / UNB magic prefix; anything else is treated as a file path. Inline content has a NULL source_path. Parsing is 100% local — no outbound calls. Use `mode =>` to force path versus content.
Named arguments
mode := VARCHAR
Force how `input` is interpreted: 'path' (read file(s)/glob) or 'content' (parse the value inline). Omit to auto-detect from the ISA/UNA/UNB magic prefix.
Returns columns
interchange_ctrl VARCHAR
ISA13 — interchange control number of the row's interchange.
group_ctrl VARCHAR
GS06 — functional group control number of the row's group.
transaction_ctrl VARCHAR
ST02 — transaction set control number of the row's transaction.
transaction_type VARCHAR
ST01 — transaction set identifier ('835','837',…); UNH02 message type for EDIFACT.
source_path VARCHAR
The file the row came from; NULL for inline content.
beg_purpose_code VARCHAR
BEG01 — transaction set purpose code (raw).
beg_po_type VARCHAR
BEG02 — purchase order type code (raw).
beg_po_number VARCHAR
BEG03 — purchase order number.
beg_date VARCHAR
BEG05 — purchase order date (raw CCYYMMDD).
n1_entity_code VARCHAR
N101 of the most recent N1 — entity identifier code (raw, e.g. BT/ST/SU/VN).
n1_name VARCHAR
N102 of the most recent N1 — party name (raw).
n1_id_qualifier VARCHAR
N103 of the most recent N1 — identification code qualifier (raw).
n1_id VARCHAR
N104 of the most recent N1 — party identification code.
per_contact_function VARCHAR
PER01 of the most recent PER — contact function code (raw).
per_name VARCHAR
PER02 of the most recent PER — contact name.
per_comm_qualifier VARCHAR
PER03 of the most recent PER — communication number qualifier (raw).
per_comm_number VARCHAR
PER04 of the most recent PER — communication number.
po1_line_number VARCHAR
PO101 — assigned line item identifier.
po1_quantity VARCHAR
PO102 — quantity ordered (raw VARCHAR).
po1_uom VARCHAR
PO103 — unit/basis of measurement code (raw).
po1_unit_price VARCHAR
PO104 — unit price (raw VARCHAR).
po1_product_qualifier VARCHAR
PO106 — product/service ID qualifier (raw).
po1_product_id VARCHAR
PO107 — product/service identifier.
Example

Project an inline 850 interchange through the read_850 shaped view.

SELECT po1_line_number, po1_product_id, po1_quantity
FROM x12.main.read_850('ISA*00*          *00*          *ZZ*SENDER         *ZZ*RECEIVER       *240101*1200*^*00501*000000001*0*P*:~GS*XX*SEND*RECV*20240101*1200*1*X*005010~ST*850*0001~BEG*00*SA*PO9988**20240101~N1*ST*ACME WAREHOUSE*92*DC07~PER*BD*JANE BUYER*TE*5551234~PO1*1*10*EA*4.50**VP*WIDGET-A~PO1*2*5*EA*9.99**VP*WIDGET-B~SE*7*0001~GE*1*1~IEA*1*000000001~');
Link
read_997 (input ANY, mode := VARCHAR) 1 example

Shaped positional view of X12 transaction set 997

Positional arguments
input ANY
The EDI to parse, overloaded across three auto-detected input modes: a file path or glob (e.g. '/data/claims/*.837'), or a list of paths — the streaming hot path, read locally in sorted order; OR an inline interchange string; OR inline interchange bytes. Inline content is recognized by its ISA / UNA / UNB magic prefix; anything else is treated as a file path. Inline content has a NULL source_path. Parsing is 100% local — no outbound calls. Use `mode =>` to force path versus content.
Named arguments
mode := VARCHAR
Force how `input` is interpreted: 'path' (read file(s)/glob) or 'content' (parse the value inline). Omit to auto-detect from the ISA/UNA/UNB magic prefix.
Returns columns
interchange_ctrl VARCHAR
ISA13 — interchange control number of the row's interchange.
group_ctrl VARCHAR
GS06 — functional group control number of the row's group.
transaction_ctrl VARCHAR
ST02 — transaction set control number of the row's transaction.
transaction_type VARCHAR
ST01 — transaction set identifier ('835','837',…); UNH02 message type for EDIFACT.
source_path VARCHAR
The file the row came from; NULL for inline content.
ak1_functional_id VARCHAR
AK1*01 — functional identifier code of the acknowledged group (raw).
ak1_group_control VARCHAR
AK1*02 — group control number being acknowledged.
ak9_status VARCHAR
AK9*01 — functional group acknowledge code (raw, A/E/R/...).
ak9_sets_included VARCHAR
AK9*02 — number of transaction sets included (raw).
ak9_sets_received VARCHAR
AK9*03 — number of received transaction sets (raw).
ak9_sets_accepted VARCHAR
AK9*04 — number of accepted transaction sets (raw).
ak2_transaction_set_id VARCHAR
AK2*01 — transaction set identifier code being acknowledged (raw).
ak2_transaction_control VARCHAR
AK2*02 — transaction set control number being acknowledged.
ak5_status VARCHAR
AK5*01 — transaction set acknowledge code (raw, A/E/R/...).
ak3_segment_id VARCHAR
AK3*01 — segment ID in error (raw).
ak3_segment_position VARCHAR
AK3*02 — segment position in the transaction set (raw).
ak3_error_code VARCHAR
AK3*04 — segment syntax error code (raw).
ak4_element_position VARCHAR
AK4*01 — element position in the segment (raw, composite).
ak4_error_code VARCHAR
AK4*03 — data element syntax error code (raw).
Example

Project an inline 997 interchange through the read_997 shaped view.

SELECT ak2_transaction_control, ak5_status, ak3_segment_id
FROM x12.main.read_997('ISA*00*          *00*          *ZZ*SENDER         *ZZ*RECEIVER       *240101*1200*^*00501*000000001*0*P*:~GS*XX*SEND*RECV*20240101*1200*1*X*005010~ST*997*0001~AK1*HC*1~AK2*837*0001~AK3*CLM*22**8~AK4*1*1028*1~AK5*E~AK2*837*0002~AK5*A~AK9*P*2*2*1~SE*10*0001~GE*1*1~IEA*1*000000001~');
Link
read_999 (input ANY, mode := VARCHAR) 1 example

Shaped positional view of X12 transaction set 999

Positional arguments
input ANY
The EDI to parse, overloaded across three auto-detected input modes: a file path or glob (e.g. '/data/claims/*.837'), or a list of paths — the streaming hot path, read locally in sorted order; OR an inline interchange string; OR inline interchange bytes. Inline content is recognized by its ISA / UNA / UNB magic prefix; anything else is treated as a file path. Inline content has a NULL source_path. Parsing is 100% local — no outbound calls. Use `mode =>` to force path versus content.
Named arguments
mode := VARCHAR
Force how `input` is interpreted: 'path' (read file(s)/glob) or 'content' (parse the value inline). Omit to auto-detect from the ISA/UNA/UNB magic prefix.
Returns columns
interchange_ctrl VARCHAR
ISA13 — interchange control number of the row's interchange.
group_ctrl VARCHAR
GS06 — functional group control number of the row's group.
transaction_ctrl VARCHAR
ST02 — transaction set control number of the row's transaction.
transaction_type VARCHAR
ST01 — transaction set identifier ('835','837',…); UNH02 message type for EDIFACT.
source_path VARCHAR
The file the row came from; NULL for inline content.
ak1_functional_id VARCHAR
AK1*01 — functional identifier code of the acknowledged group (raw).
ak1_group_control VARCHAR
AK1*02 — group control number being acknowledged.
ak9_status VARCHAR
AK9*01 — functional group acknowledge code (raw).
ak9_sets_included VARCHAR
AK9*02 — number of transaction sets included (raw).
ak9_sets_received VARCHAR
AK9*03 — number of received transaction sets (raw).
ak9_sets_accepted VARCHAR
AK9*04 — number of accepted transaction sets (raw).
ak2_transaction_set_id VARCHAR
AK2*01 — transaction set identifier code being acknowledged (raw).
ak2_transaction_control VARCHAR
AK2*02 — transaction set control number being acknowledged.
ik5_status VARCHAR
IK5*01 — transaction set acknowledge code (raw).
ik3_segment_id VARCHAR
IK3*01 — segment ID in error (raw).
ik3_segment_position VARCHAR
IK3*02 — segment position in the transaction set (raw).
ik3_error_code VARCHAR
IK3*04 — implementation segment syntax error code (raw).
ik4_element_position VARCHAR
IK4*01 — element position in the segment (raw, composite).
ik4_error_code VARCHAR
IK4*03 — implementation data element syntax error code (raw).
Example

Project an inline 999 interchange through the read_999 shaped view.

SELECT ak2_transaction_control, ik3_segment_id, ik5_status
FROM x12.main.read_999('ISA*00*          *00*          *ZZ*SENDER         *ZZ*RECEIVER       *240101*1200*^*00501*000000001*0*P*:~GS*XX*SEND*RECV*20240101*1200*1*X*005010~ST*999*0001~AK1*HC*1~AK2*837*0001~IK3*NM1*8**8~IK4*2*1037*7~IK5*R~AK9*R*1*1*0~SE*8*0001~GE*1*1~IEA*1*000000001~');
Link
read_x12 (input ANY, mode := VARCHAR) 1 example

Read complete X12 segment rows, including envelopes, ordering, and delimiter metadata.

Positional arguments
input ANY
The EDI to parse, overloaded across three auto-detected input modes: a file path or glob (e.g. '/data/claims/*.837'), or a list of paths — the streaming hot path, read locally in sorted order; OR an inline interchange string; OR inline interchange bytes. Inline content is recognized by its ISA / UNA / UNB magic prefix; anything else is treated as a file path. Inline content has a NULL source_path. Parsing is 100% local — no outbound calls. Use `mode =>` to force path versus content.
Named arguments
mode := VARCHAR
Force how `input` is interpreted: 'path' (read file(s)/glob) or 'content' (parse the value inline). Omit to auto-detect from the ISA/UNA/UNB magic prefix.
Returns columns
interchange_ctrl VARCHAR
ISA13 — interchange control number of the row's interchange.
group_ctrl VARCHAR
GS06 — functional group control number of the row's group.
transaction_ctrl VARCHAR
ST02 — transaction set control number of the row's transaction.
transaction_type VARCHAR
ST01 — transaction set identifier ('835','837',…); UNH02 message type for EDIFACT.
source_path VARCHAR
The file the row came from; NULL for inline content.
document_index BIGINT
Zero-based document ordinal within this invocation; distinguishes inline and repeated input paths.
segment_index BIGINT
Zero-based segment ordinal across the whole document, including all envelopes. Order by document_index, segment_index before COPY TO.
segment_id VARCHAR
Raw segment identifier, including ISA, GS, ST, SE, GE, and IEA.
elements VARCHAR[]
All positional element values, preserving empty values, padding, composites, and repetitions as unsplit text.
byte_offset BIGINT
Start byte offset in the source document; informational after edits.
element_separator VARCHAR
Element separator in force for this row's interchange.
segment_terminator VARCHAR
Segment terminator in force for this row's interchange. Component and repetition separators remain in ISA16 and ISA11.
Example

Read all segments of an inline X12 interchange, including its envelopes.

SELECT segment_index, segment_id, elements
FROM x12.main.read_x12('ISA*00*          *00*          *ZZ*SENDER         *ZZ*RECEIVER       *240101*1200*^*00501*000000001*0*P*:~GS*XX*SEND*RECV*20240101*1200*1*X*005010~ST*837*0001~CLM*ACCT1*500~SE*3*0001~GE*1*1~IEA*1*000000001~');
Link
segments (input ANY, mode := VARCHAR) 1 example

Explode an X12 interchange into one row per segment (elements as a LIST)

Positional arguments
input ANY
The EDI to parse, overloaded across three auto-detected input modes: a file path or glob (e.g. '/data/claims/*.837'), or a list of paths — the streaming hot path, read locally in sorted order; OR an inline interchange string; OR inline interchange bytes. Inline content is recognized by its ISA / UNA / UNB magic prefix; anything else is treated as a file path. Inline content has a NULL source_path. Parsing is 100% local — no outbound calls. Use `mode =>` to force path versus content.
Named arguments
mode := VARCHAR
Force how `input` is interpreted: 'path' (read file(s)/glob) or 'content' (parse the value inline). Omit to auto-detect from the ISA/UNA/UNB magic prefix.
Returns columns
interchange_ctrl VARCHAR
ISA13 — interchange control number of the row's interchange.
group_ctrl VARCHAR
GS06 — functional group control number of the row's group.
transaction_ctrl VARCHAR
ST02 — transaction set control number of the row's transaction.
transaction_type VARCHAR
ST01 — transaction set identifier ('835','837',…); UNH02 message type for EDIFACT.
source_path VARCHAR
The file the row came from; NULL for inline content.
segment_index BIGINT
0-based ordinal of this segment within its ST..SE transaction (ST is 0).
segment_id VARCHAR
Raw segment identifier (e.g. 'CLP', 'NM1', 'HL').
elements VARCHAR[]
Positional data-element values of the segment (the values after the segment ID).
byte_offset BIGINT
Start offset of the segment in the source document (debug/replay).
Example

Explode an inline 837 claim interchange into one row per segment.

SELECT segment_index, segment_id, elements
FROM x12.main.segments('ISA*00*          *00*          *ZZ*SENDER         *ZZ*RECEIVER       *240101*1200*^*00501*000000001*0*P*:~GS*XX*SEND*RECV*20240101*1200*1*X*005010~ST*837*0001~BHT*0019*00*REF01*20240101*1200*CH~NM1*85*2*BILLING CLINIC*****XX*1122334455~CLM*ACCT777*500***11:B:1*Y~HI*ABK:Z1234~SV1*HC:99213*200*UN*1~SE*7*0001~GE*1*1~IEA*1*000000001~');
Link
segments_elements (input ANY, mode := VARCHAR) 1 example

Explode an X12 interchange into one row per element (component/repetition split)

Positional arguments
input ANY
The EDI to parse, overloaded across three auto-detected input modes: a file path or glob (e.g. '/data/claims/*.837'), or a list of paths — the streaming hot path, read locally in sorted order; OR an inline interchange string; OR inline interchange bytes. Inline content is recognized by its ISA / UNA / UNB magic prefix; anything else is treated as a file path. Inline content has a NULL source_path. Parsing is 100% local — no outbound calls. Use `mode =>` to force path versus content.
Named arguments
mode := VARCHAR
Force how `input` is interpreted: 'path' (read file(s)/glob) or 'content' (parse the value inline). Omit to auto-detect from the ISA/UNA/UNB magic prefix.
Returns columns
interchange_ctrl VARCHAR
ISA13 — interchange control number of the row's interchange.
group_ctrl VARCHAR
GS06 — functional group control number of the row's group.
transaction_ctrl VARCHAR
ST02 — transaction set control number of the row's transaction.
transaction_type VARCHAR
ST01 — transaction set identifier ('835','837',…); UNH02 message type for EDIFACT.
source_path VARCHAR
The file the row came from; NULL for inline content.
segment_index BIGINT
0-based ordinal of this element's segment within its ST..SE transaction.
segment_id VARCHAR
Raw segment identifier (e.g. 'CLM', 'NM1').
element_index INTEGER
1-based element position (so CLP02 is element_index = 2).
component_index INTEGER
1-based sub-element position; NULL unless the element is a composite.
repetition_index INTEGER
1-based repetition occurrence; NULL unless the element repeats.
value VARCHAR
The raw element/component value (EDIFACT release char applied).
Example

Explode an inline 837 CLM segment into element/component rows (CLM05 is the composite '11:B:1').

SELECT segment_id, element_index, component_index, value
FROM x12.main.segments_elements('ISA*00*          *00*          *ZZ*SENDER         *ZZ*RECEIVER       *240101*1200*^*00501*000000001*0*P*:~GS*XX*SEND*RECV*20240101*1200*1*X*005010~ST*837*0001~CLM*ACCT777*500***11:B:1*Y~HI*ABK:Z1234~SV1*HC:99213*200*UN*1~SE*5*0001~GE*1*1~IEA*1*000000001~');
Link

COPY formats

Attaching x12 also registers a file format for DuckDB's own COPY statement, so you can load files into local tables and write query results out to files without a function call. The FORMAT name starts with the ATTACH alias — x12 with the Quick start above; use yours if you attach it under another name.

FORMAT 'x12.x12'

COPY … FROM · import COPY … TO · export

X12 COPY. Complete X12 segment rows (read_x12 schema)

copy.sql
-- Export an X12 interchange's segment rows to a file through the X12 COPY format, in document and segment order.
COPY (SELECT * FROM x12.main.read_x12('ISA*00*          *00*          *ZZ*SENDER         *ZZ*RECEIVER       *240101*1200*^*00501*000000001*0*P*:~GS*XX*SEND*RECV*20240101*1200*1*X*005010~ST*997*0001~AK1*PO*1~AK9*A*1*1*1~SE*4*0001~GE*1*1~IEA*1*000000001~') ORDER BY document_index, segment_index) TO 'acknowledgment.x12' (FORMAT 'x12.x12');

Options

Pass these inside the COPY statement's parentheses, after FORMAT. Unknown options are rejected.

recalculate_countsBOOLEAN
Recompute SE01, GE01, and IEA01 after edits. Defaults to false (validate existing counts). Never changes control numbers or synthesizes missing envelopes.
Full reference
X12 COPY format

The registered handler is invoked by DuckDB's COPY machinery. It cannot be called directly as a table function; name it as FORMAT '<alias>.x12' ('x12.x12' when attached as x12).

Import. Create the destination table from the schema of read_x12(input) with zero rows, then import a worker-local file or glob. Column names, order, and types must match. Reads retain malformed segments for inspection and materialize the input in memory.

Export. Supply the four named columns segment_id, elements, element_separator, and segment_terminator, ordered by document and segment index. Other columns are informational. ISA11 and ISA16 preserve repetition and component delimiters. Empty element strings are retained; NULL values are rejected.

Validation and publication. The writer checks ASCII text, fixed-width ISA, envelope nesting, counts, and control matches across batches. The explicit recalculate_counts option updates SE01/GE01/IEA01 only. A same-directory temporary file replaces the target after validation completes; failure preserves the old target. Empty input writes an empty file. This is structural validation; transaction-specific content rules remain outside scope. Formatting whitespace is normalized. EDIFACT and binary payloads are unsupported. See the repository README for the complete edit-and-export workflow.

Try these queries

Flag transactions whose SE segment count does not validate: this inline 837 declares 9 segments in SE01 but carries 4.

SELECT interchange_ctrl, transaction_type, transaction_ctrl, segment_count,
  se_count_ok
FROM x12.main.envelope('ISA*00*          *00*          *ZZ*SENDER         *ZZ*RECEIVER       *240101*1200*^*00501*000000001*0*P*:~GS*XX*SEND*RECV*20240101*1200*1*X*005010~ST*837*0001~BHT*0019*00*REF01*20240101*1200*CH~CLM*ACCT777*500***11:B:1*Y~SE*9*0001~GE*1*1~IEA*1*000000001~')
WHERE se_count_ok IS DISTINCT FROM TRUE;

Uses envelope

Extract claim id, total paid, and payer from an inline 835 remittance interchange.

SELECT clp_claim_id, clp_total_paid, payer_name
FROM x12.main.read_835('ISA*00*          *00*          *ZZ*SENDER         *ZZ*RECEIVER       *240101*1200*^*00501*000000001*0*P*:~GS*XX*SEND*RECV*20240101*1200*1*X*005010~ST*835*0001~BPR*I*1500*C*ACH~TRN*1*TRACE0001*1512345678~NM1*PR*2*ACME HEALTH PLAN*****PI*PAYER001~NM1*PE*2*WELLNESS CLINIC LLC*****XX*1999999999~CLP*PCN1001*1*500*400*100*MC*CCN0001*11~CAS*CO*45*100~CLP*PCN1002*4*1000*0*0*MC*CCN0002*11~SE*9*0001~GE*1*1~IEA*1*000000001~');

Uses read_835

Classify sample EDI files (committed in this repo, pinned to a release tag) by transaction set type for routing.

SELECT filename, x12.main.transaction_type(content) AS tx_type
FROM read_text(['https://raw.githubusercontent.com/Query-farm/vgi-x12/v0.5.0/data/claim_837.837', 'https://raw.githubusercontent.com/Query-farm/vgi-x12/v0.5.0/data/era_835.835', 'https://raw.githubusercontent.com/Query-farm/vgi-x12/v0.5.0/data/po_850.850', 'https://raw.githubusercontent.com/Query-farm/vgi-x12/v0.5.0/data/orders_edifact.edi'])
ORDER BY filename;

Uses transaction_type

Count exploded EDIFACT elements per message type in an inline ORDERS interchange.

SELECT transaction_type, count(*) AS n_elements
FROM x12.main.edifact_segments('UNA:+.? ''UNB+UNOA:1+SENDER+RECEIVER+240101:1200+REF1''UNH+MSG1+ORDERS:D:96A:UN''BGM+220+PO12345+9''DTM+137:20240101:102''NAD+BY+++ACME CORP''UNT+4+MSG1''UNZ+1+REF1''')
GROUP BY transaction_type;

Uses edifact_segments

Reconcile billed claims (read_837) against the payer's remittance (read_835) by claim id: ACCT777 billed 500 and paid 400 (CLP02 status 1, 100 unpaid), ACCT778 billed 1000 and paid 0 (CLP02 status 4), ACCT779 billed 250 with no remittance yet (NULL status and paid).

WITH billed AS (SELECT DISTINCT clm_id, CAST(clm_total_charge AS DECIMAL(12, 2)) AS billed FROM x12.main.read_837('ISA*00*          *00*          *ZZ*CLINIC         *ZZ*PAYER          *240101*1200*^*00501*000000201*0*P*:~GS*HC*CLINIC*PAYER*20240101*1200*1*X*005010X222A1~ST*837*0001~BHT*0019*00*REF01*20240101*1200*CH~NM1*85*2*BILLING CLINIC*****XX*1122334455~NM1*IL*1*DOE*JOHN****MI*MEMBER123~CLM*ACCT777*500***11:B:1*Y~SV1*HC:99213*200*UN*1~SV1*HC:85025*300*UN*1~CLM*ACCT778*1000***11:B:1*Y~SV1*HC:99214*1000*UN*1~CLM*ACCT779*250***11:B:1*Y~SV1*HC:99212*250*UN*1~SE*12*0001~GE*1*1~IEA*1*000000201~')), paid AS (SELECT DISTINCT clp_claim_id, clp_status_code, CAST(clp_total_paid AS DECIMAL(12, 2)) AS paid FROM x12.main.read_835('ISA*00*          *00*          *ZZ*PAYER          *ZZ*CLINIC         *240115*0900*^*00501*000000301*0*P*:~GS*HP*PAYER*CLINIC*20240115*0900*1*X*005010X221A1~ST*835*0001~BPR*I*400*C*ACH~CLP*ACCT777*1*500*400*100*MC*CCN0001*11~CAS*PR*1*100~CLP*ACCT778*4*1000*0*0*MC*CCN0002*11~CAS*CO*50*1000~SE*7*0001~GE*1*1~IEA*1*000000301~')) SELECT b.clm_id, b.billed, p.clp_status_code, p.paid, b.billed - coalesce(p.paid, 0) AS unpaid
FROM billed AS b
LEFT JOIN paid AS p ON p.clp_claim_id = b.clm_id
ORDER BY b.clm_id;

Uses read_837read_835

List the shaped X12 transaction sets and their read_* function.

SELECT transaction_set, shaped_function
FROM x12.main.transaction_sets
WHERE family = 'X12'
ORDER BY transaction_set;

Uses transaction_sets

20 more examples in the Catalog, each listed under the table or function it uses.

X12 EDI 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.