# Convert string to varbinary(max)

**URL:** <https://forum.servoy.com/t/convert-string-to-varbinary-max/21005>\
**Category:** Questions\
**Created:** [May 25, 2023, 9:24am UTC](https://forum.servoy.com/t/convert-string-to-varbinary-max/21005 "2023-05-25T09:24:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hpmxxx](https://avatars.discourse-cdn.com/v4/letter/h/ec9cab/32.png) [@hpmxxx](https://forum.servoy.com/u/hpmxxx)\
**Post date:** [May 25, 2023, 9:24am UTC](https://forum.servoy.com/t/convert-string-to-varbinary-max/21005/1 "2023-05-25T09:24:04Z")

</div>

Hello all,

I am having an issue saving a string to a database column of type varbinary(max) in a Microsoft SQL Server 2019.

I receive a hash value as a string via a REST endpoint. Now when I try to save this to the DB, I get the following exception:

```auto
com.microsoft.sqlserver.jdbc.SQLServerException: Implicit conversion from data type nvarchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.

```

The exception is thrown by the following code:

```auto
var hash = '0xF6A78BA60C568C3668A6E419E9C1215A80D478CD511F892DFFDEAB051'
var sql = 'UPDATE TABLE SET TABLE.COLUMN = ? WHERE TABLE.PK = ?';
var sqlParam = [hash, pk];
var sqlSuccess = plugins.rawSQL.executeSQL(SERVERNAME, sql, sqlParam);

```

For example, if I convert the string as follows, the hash string is converted incorrectly:

```auto
var sql = 'UPDATE TABLE SET TABLE.COLUMN = CAST(? AS varbinary(max)) WHERE TABLE.PK = ?';

```

The result looks like this:

```auto
TABLE.COLUMN: 0x3078463641373842413630433536384333363638413645343139453943313231354138304434373843443531314638393244464644454142303531

```

“CONVERT” creates the same problem as “CAST”.

Does anyone know how to convert a string so that hash and TABLE.COLUMN are identical?

Servoy 2020.3.3

---

<div class="post-metadata">

**Author:** ![hpmxxx](https://avatars.discourse-cdn.com/v4/letter/h/ec9cab/32.png) [@hpmxxx](https://forum.servoy.com/u/hpmxxx)\
**Post date:** [May 25, 2023, 5:11pm UTC](https://forum.servoy.com/t/convert-string-to-varbinary-max/21005/2 "2023-05-25T17:11:19Z")

</div>

I found the solution.

```auto
CONVERT(varbinary(max), ?, 1)

```

The 1 is the key. The default value in MsSql is 0.
