Add default value to a Varbinary column in SQL Server

80 Views Asked by At

I am trying to set a default value for a column DefaultImage in a SQL Server table which is defined as Varbinary(MAX). I've attempted to add the default value both from SSMS and Visual Studio.

[DefaultImage] [varbinary](max) NOT NULL

However, I am facing a specific problem: when the value I am trying to set is an image with low detail and the length is even, it is stored correctly. But if the image is larger, more detailed, and has an odd length, the value is stored incorrectly and an extra "0" is added.

To illustrate it better https://dbfiddle.uk/ETF0pWuR

Correct value:   0x89504E470D0A1A0A0000000D49484452000007...
Incorrect value: 0x089504E470D0A1A0A0000000D49484452000007...

As you can see, in the incorrect value, an extra 0 has been added. When I try to decode it from my C# application, an error message is generated indicating that the value is invalid.

If I load the same image from my application and save it in the ImageDefault column from the application, it works correctly. The problem only occurs when adding the default value.

Please can you tell me what I can do to add this value by default

0

There are 0 best solutions below