Explainer
Why a varbinary historian value cannot be decoded from SQL alone
Short answer
A varbinary column fixes storage capacity, not field layout, so the rows are the OPC UA server's private serialization and SQL alone cannot decode them. Read the history with OPC UA Historical Access (IEC 62541-11), or decode in SQL only when the vendor documents the layout for the installed build or controlled writes of 0x0000, 0x0001 and 0x0100 have pinned the offset and byte order.

On this page
- What does a varbinary(4096) column actually define?
- What is inside those 32-byte records?
- How do you read the history without decoding the blob?
- When is decoding the blob in SQL safe?
- How do you find the UINT16 field with controlled writes?
- How do you verify a decoder before trusting it?
- What is the decision for each case?
- Questions asked
A historian row whose value column holds an opaque varbinary(4096) blob is not a number in disguise. It is the OPC UA server's private serialization of its own history store, and SQL Server carries no metadata that names a value field, a timestamp field or a quality word inside those bytes, so decoding from SQL alone fails on missing information, not on skill. The supported path is the standard one: OPC UA Historical Access, defined in IEC 62541-11, read from the same server that fills the table, which returns value, source timestamp and quality already decoded. Decode in SQL only where the vendor documents the record layout for the installed build, or where controlled writes of known values have pinned the offset and byte order byte by byte. What follows: what such records reveal, what they withhold, the test that settles a value field, and the decision for each case.
What does a varbinary(4096) column actually define? #
A varbinary(4096) column promises capacity and nothing else: it can hold a value of up to 4,096 bytes per row, and it carries no information about field offsets, byte order, timestamps, or quality inside those bytes.
Capacity is the only promise. The type does not fix how many bytes a record uses — the rows in question hold a small fraction of it — and it does not fix what any byte means. SQL Server can slice, compare and pattern-match binary data and reports how many bytes each row actually stores, but none of that assigns meaning. The meaning of every byte range is decided by the serialization the server chose, which lives outside the database.
That serialization belongs to the server build that produced the rows: change the build and the layout can change with it. The only authoritative description is the vendor's documentation for the build you run. A layout inferred from one set of rows stays a hypothesis, however consistent it looks.
What is inside those 32-byte records? #
The only honest reading of one observed set of records — 32 bytes each stored in that 4,096-byte column — is a map of what changes and what repeats, because no byte range can be labeled value, timestamp or quality without the serialization contract.
The tag behind the observed rows is a UINT16, and one real record from the set reads:
0x00053C17000000003C83D501DE5F20853C83D501DE5F20850000000065000000
Splitting the rows into byte ranges gives this map:
| Bytes | Observed across the rows | What it establishes |
|---|---|---|
| 1-8 | Leading pair constant at 00 05; bytes 3-4 climb as a little-endian pair, from 3C 17 toward CB 18; bytes 5-8 are zero in every row |
A region with a fixed part and a climbing part; no meaning assignable |
| 9-16 | Eight bytes that vary from row to row | An unidentified field or field fragment |
| 17-24 | Identical to bytes 9-16 in every row, byte for byte | The duplication is confirmed; its purpose is not |
| 25-32 | Bytes 25-28 zero everywhere; bytes 29-31 vary, often at zero | One or more fields; the boundaries are unresolved |
The UINT16 occupies exactly 2 bytes, and the leading 00 05 is the tempting candidate: read big-endian it is 5, read little-endian it is 1,280, and it could equally be record metadata, because it stays constant while other regions move. The doubled region is equally unexplained — a stored copy, padding or a checksum input all fit it. Regularity is not identification, and nothing in this map licenses calling any region the process value.
How do you read the history without decoding the blob? #
OPC UA Historical Access (IEC 62541-11) is the supported path: the server that fills the SQL table also answers history reads, returning value, source timestamp and quality already decoded.
The standard has the server advertise the capability: OPC UA server SDK documentation names the attribute AccessHistoryDataCapability, and a true value on a variable says historical values are readable there. A client that supports the feature issues the history read service, HistoryRead, for a node and a time range, and receives data values with source timestamps and status codes intact. The fields the blob withholds arrive already decoded, in the server's own representation, so the offset and byte-order questions never arise.
Archiving is configured on the server, not discovered in the database: for this class of product the vendor's configurator splits the job into a history adapter, which selects the storage back end such as RAM, file or an SQL database, and a history node, which selects the variable being archived. The SQL rows are the shadow of that configuration, not an interface to it, so a change you want belongs in the server's configuration tool under the vendor's documented steps. If a variable does not advertise the capability, no history read exists for it, and a vendor-documented export becomes the only alternative to the blob.
The same client connection that reads history can feed the values onward to dashboards and other consumers, the way exposing historian latest values over OPC UA or MQTT works for current values. If a client reports decode errors on structured values, the cause is usually an encoding mismatch rather than damaged data — the same class of problem as fixing client decode errors on OPC UA enumeration encodings.
When is decoding the blob in SQL safe? #
Direct SQL decoding is safe only when the server vendor documents the record layout for the exact server build you run, because a layout confirmed for one build can change silently with the next.
A usable contract names, for that build: the field offsets and widths; the byte order; the timestamp encoding, meaning epoch, resolution and units — a 100-nanosecond tick field and a seconds field differ by a factor of 10,000,000; the quality or status representation; and a record or schema version marker, so a layout change announces itself instead of corrupting values quietly.
Historians that publish their schema show the contrast: reading default historian data in SQL Server the supported way is unambiguous because tables, columns and meanings are documented. A blob with no published layout is the opposite case, and it stays indeterminate no matter how regular the bytes look — the doubled eight-byte region above is perfectly regular and perfectly unexplained.
How do you find the UINT16 field with controlled writes? #
Three controlled writes isolate a two-byte value: write 0x0000, 0x0001 and 0x0100 to a test tag, snapshot the record after each, and the byte pair that flips as 00 00, then 01 00, then 00 01 is the little-endian UINT16 field.
Run the test on an isolated test tag in a test project, never on a live control value: a write to a production tag can move real machinery, so controlled writes stay off the plant floor.
- Hold the tag at a known constant first, so the before-state is stable.
- Snapshot the record, and confirm a new row actually appears for each write — the logging trigger, deadband, buffering and retention decide when rows land, and a missing row poisons the comparison.
- Write 0x0000 and snapshot; write 0x0001 and snapshot; write 0x0100 and snapshot.
- Compare snapshots two at a time. Regions that change in every comparison are timestamps or metadata; the byte pair that tracks the written pattern is the value.
Worked example, with an illustrative offset: say the before-snapshot holds 00 00 at bytes 26-27, counting from 1, and every other change between snapshots is timestamp-like. After the write of 0x0001 the pair reads 01 00 — exactly the low byte moved. After 0x0100 it reads 00 01 — exactly the high byte moved. The field occupies bytes 26-27 in little-endian order, and the decode rule is value = byte26 + 256 × byte27. Check it: 01 00 gives 1 + 256 × 0 = 1, and 00 01 gives 0 + 256 × 1 = 256.
Confirm the offset with one mixed value before trusting it. Write 0x1234, expect stored bytes 34 12, and decode 52 + 256 × 18 = 52 + 4,608 = 4,660, which matches 0x1234 exactly. Had the single changed byte landed one position earlier, the order would be big-endian and the rule would be value = 256 × byte26 + byte27.
Once the offset is proven, reading meaning out of those two bytes is ordinary work — the same skill as decoding uint16 status bits in a dashboard.
How do you verify a decoder before trusting it? #
A decoder is trustworthy after it reproduces every reference value exactly: boundary writes of 0x0000 and 0xFFFF, one mixed pattern such as 0x1234, and byte-identical output whenever the input binary is identical.
- Boundary values: 0xFFFF is 65,535 and stores as
FF FF; require the decoded output to equal 0 and 65,535 exactly, with no clipping. - Independent reference: pull the same time range through a history read and require exact agreement on value, timestamp and quality for every record in the window — the server's own decode is the referee.
- Determinism: identical binary input must produce identical decoded output on every run.
- Timestamp and quality: they change on their own schedule, so the value-write test does not pin them; take their layout only from documented widths or from the reference comparison, never from a guess about the doubled region.
- Preservation: read the varbinary column, never rewrite it, and store decoded columns alongside so every decoded row traces back to its source bytes.
- Re-verification: pin the decoder to the documented server build and rerun the boundary and reference tests after every upgrade, because a layout belongs to a build, not to the table.
What is the decision for each case? #
Commit by case: a documented layout means decode in SQL and verify against history reads; an undocumented layout means treat the column as opaque and read through the server; a hard SQL requirement for reporting means writing into your own table.
- Vendor documents the layout for your installed build: decode in SQL Server, verify against a history-read reference window before anyone consumes the numbers, and rerun that verification after every server upgrade.
- No documented layout: treat the blob as opaque. Use an OPC UA historical access client or a vendor-documented export and leave the varbinary column untouched.
- Reports and joins must live in SQL: run your own collector that reads the values over OPC UA and inserts them into a table whose schema you define — the same pattern as logging PLC values from an OPC server into SQL Server. Your own columns need no reverse engineering, survive server upgrades unchanged, and turn every later query into ordinary SQL.
The blob is the server's internal store, not an interface. The standard gives you the interface — the history read — and every legitimate shortcut starts from it.
Questions asked
Does every row use the full varbinary capacity?
No. The type fixes the maximum, not the record size, and the observed rows carried 32 bytes each. SQL Server reports each row's stored byte count directly, so group rows by stored length before comparing offsets, because two different record layouts can coexist at different lengths in the same table.
Why do bytes 9-16 and bytes 17-24 repeat each other?
The duplication is real in the observed rows, but its meaning is not established: a stored copy, padding and a checksum input are all consistent with it. Treat both regions as unknown fields until a controlled change or vendor documentation assigns a meaning, and never label either as the timestamp on visual similarity alone.
Can SQL Server's binary functions decode the value?
They convert and manipulate bytes, but conversion is not decoding: nothing in SQL Server assigns an offset, a byte order, a timestamp encoding or a quality meaning to an opaque blob. A supported decode exists only where the vendor publishes the layout for the installed build; otherwise the supported reads are history reads or a documented export.
What breaks a decoder that passed verification earlier?
Most often a server upgrade: the record layout belongs to the server build, and a new build can shift an offset or change a timestamp encoding silently. Re-run the boundary writes and the history-read comparison after every upgrade, and record the build the decoder was verified against so mismatches surface instead of corrupting values.


