20

I am trying to insert 0 value in varbinary max to test some results. I tried casting '' to binary, tried entering 0 to it but it converts it to 0x00.

Is there a way to enter just 0 for particular value?

4
  • 0x00 is the hexadecimal representation of 0. Oct 18, 2013 at 20:20
  • @MattGrande I think OP means an empty byte sequence.
    – Matthew
    Oct 18, 2013 at 20:20
  • @MattGrande - is there a way to get just 0?
    – NoviceMe
    Oct 18, 2013 at 20:20
  • It IS just 0. If you say SELECT * FROM MyTable WHERE MyBinaryField = 0, your row will show up. 0x00 is just how it's displayed. Oct 18, 2013 at 20:26

2 Answers 2

48

If you're looking to represent an empty byte sequence, set its value to 0x

INSERT INTO MyTable(MyBinary) VALUES (0x)

For example:

DECLARE @myTable TABLE (
    binaryField VARBINARY(MAX) NOT NULL
)

INSERT INTO @myTable(binaryField) VALUES(0x) -- 0 bytes
INSERT INTO @myTable(binaryField) VALUES(0x11223344) -- 4 bytes

SELECT *, len(binaryField) FROM @myTable
2
  • it enters 0x still not just 0.
    – NoviceMe
    Oct 18, 2013 at 20:23
  • 6
    I think the problem lies with how you're trying to represent the data in the column, not the storage of the data itself. 0x is the representation of an empty byte sequence.
    – Matthew
    Oct 18, 2013 at 20:24
0

Try the following:

DECLARE @binaryField VARBINARY(MAX);
SET @binaryField = 0x00000000;
SELECT CONVERT(INT, @binaryField)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.