Bitwise retrieval for process data
- Last UpdatedMar 19, 2025
- 2 minute read
It is common to pack multiple digital states into the same PLC register as an integer rather than as individual bits. You can still map the individual bits to separate Historian tags for most DAServers/PLCs, but if you instead map the entire integer to a single Historian tag, you can address individual bits using standard SQL Server queries.
For example, consider the following query that returns process data values for the ’SysTimeMin’ tag:
SELECT Value FROM dbo.History WHERE TagName = 'SysTimeMin'
However to get more bits of data, you can add 2 (bitposition-1) and use bitwise & operator on the Value column as shown in the following query. Using the the Integer cast, you can query a maximum of 32 bits.
SELECT
CONVERT(BIT, CAST(Value AS INT) & 1) As 'Bit0',
CONVERT(BIT, CAST(Value AS INT) & 2) As 'Bit1',
CONVERT(BIT, CAST(Value AS INT) & 4) As 'Bit2',
CONVERT(BIT, CAST(Value AS INT) & 8) As 'Bit3',
CONVERT(BIT, CAST(Value AS INT) & 16) As 'Bit4',
CONVERT(BIT, CAST(Value AS INT) & 32) As 'Bit5',
CONVERT(BIT, CAST(Value AS INT) & 64) As 'Bit6',
CONVERT(BIT, CAST(Value AS INT) & 128) As 'Bit7'
FROM dbo.History WHERE TagName = 'SysTimeMin'
The results are:
|
Bit0 |
Bit1 |
Bit2 |
Bit3 |
Bit4 |
Bit5 |
Bit6 |
Bit7 |
|
0 |
1 |
1 |
0 |
0 |
0 |
0 |
0 |
|
1 |
1 |
1 |
0 |
0 |
0 |
0 |
0 |
|
0 |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
|
1 |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
|
0 |
1 |
0 |
1 |
0 |
0 |
0 |
0 |
|
1 |
1 |
0 |
1 |
0 |
0 |
0 |
0 |
|
0 |
0 |
1 |
1 |
0 |
0 |
0 |
0 |
|
1 |
0 |
1 |
1 |
0 |
0 |
0 |
0 |
|
0 |
1 |
1 |
1 |
0 |
0 |
0 |
0 |
|
1 |
1 |
1 |
1 |
0 |
0 |
0 |
0 |