# Convert SQL Server varbinary(max) to Float

**URL:** <https://forum.inductiveautomation.com/t/convert-sql-server-varbinary-max-to-float/109612>\
**Category:** Ignition\
**Tags:** database, scripting\
**Created:** [August 11, 2025, 8:43pm UTC](https://forum.inductiveautomation.com/t/convert-sql-server-varbinary-max-to-float/109612 "2025-08-11T20:43:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lynn\_Martineau](https://avatars.discourse-cdn.com/v4/letter/l/f1d935/32.png) [@Lynn\_Martineau](https://forum.inductiveautomation.com/u/Lynn_Martineau)\
**Post date:** [August 11, 2025, 8:43pm UTC](https://forum.inductiveautomation.com/t/convert-sql-server-varbinary-max-to-float/109612/1 "2025-08-11T20:43:38Z")

</div>

I have a float value stored in a SQL Server database as varbinary(max) [from a COTS product] that I need to retrieve in Ignition 8.1.47.

How can I get the float value?

I’m getting different values using CONVERT and CAST and it looks like I’ll need a script to convert it to a Float after I get something I trust from the database. The **value** , **Convert1** and **Convert2** have different values with every execution though the database value remains the same.

SQL Server value = 0x014052C00000000000

Named Query:

```python
SELECT TOP(1)
    ,value
    ,CONVERT(varbinary(64), value, 1) AS Convert1
    ,CONVERT(varbinary(64), value, 2) AS Convert2
    ,CAST(value AS nvarchar(max)) AS Cast1
    ,CAST(value AS varchar(120)) AS Cast2
FROM property_vtq
WHERE name = 'TargetValue'
AND id = :name

```

Sample output of the named query:

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/9/09ed2f54badc3ef448ffa5b2c6541d5eb1fbb735.png)

---

<div class="post-metadata">

**Author:** ![Lynn\_Martineau](https://avatars.discourse-cdn.com/v4/letter/l/f1d935/32.png) [@Lynn\_Martineau](https://forum.inductiveautomation.com/u/Lynn_Martineau)\
**Post date:** [August 12, 2025, 1:25pm UTC](https://forum.inductiveautomation.com/t/convert-sql-server-varbinary-max-to-float/109612/2 "2025-08-12T13:25:34Z")

</div>

Doh! realized I was using the wrong data type in the convert statement.

```python
,CONVERT(nvarchar(100), value, 1) AS TargetString

```

Consistently gives me the same value that SQL Server shows.

Now use Jython to turn it into a Float

```python
import struct
targetString='0x014052C00000000000'
binaryData = bytearray.fromhex(targetString[2:]) # Remove '0x', convert to bytes

floatValue = struct.unpack('>d', binaryData[1:])[0]

print floatValue

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 12, 2025, 1:29pm UTC](https://forum.inductiveautomation.com/t/convert-sql-server-varbinary-max-to-float/109612/3 "2025-08-12T13:29:55Z")

</div>

If you don’t cast it at all, you should get a byte array that struct can handle directly. You can also wrap such a byte array in a Java `ByteBuffer`, then use `.readDouble()`, which should be much more efficient than the jython `struct` stdlib module.

---

<div class="post-metadata">

**Author:** ![Lynn\_Martineau](https://avatars.discourse-cdn.com/v4/letter/l/f1d935/32.png) [@Lynn\_Martineau](https://forum.inductiveautomation.com/u/Lynn_Martineau)\
**Post date:** [August 12, 2025, 1:38pm UTC](https://forum.inductiveautomation.com/t/convert-sql-server-varbinary-max-to-float/109612/4 "2025-08-12T13:38:05Z")

</div>

Thanks.

I’m still confused that without a CONVERT, the NamedQuery returns a different result every time.

Here’s 5 returns from NamedQuery:

`SELECT value FROM property_vtq WHERE name = 'TargetValue' AND id = :name`

- [B@8370ee
- [B@7cee8095
- [B@53f91801
- [B@404d0384
- [B@77a3ddc

_Edit: TargetValue and id is unique, only returns one row_

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 12, 2025, 2:04pm UTC](https://forum.inductiveautomation.com/t/convert-sql-server-varbinary-max-to-float/109612/5 "2025-08-12T14:04:12Z")

</div>

The default stringification of a byte array object is its address in memory. That has no relevance to the **content**. Add `print str(binary_data)` to your script to see what I mean.
