Convert Varbinary to Int and BigInt Gives Different Results

  • If I run The query below it gives different output

    DECLARE @VB VARBINARY(8000) = CAST('0xD7DF6FC50A6807D31E27505A46DA7EA0' AS VARBINARY(8000))

    SELECT CONVERT(BIGINT, @VB)

    SELECT CONVERT(INT, @VB)

    --3762269585605476656

    --927285552

    Why is this so? The only difference here is bigint can hold more values than int but conversion gives totally different values

    Any explanation appreciated.

  • Simple, BIGINT is 8 bytes while INT is 4 bytes. Run the following code:

    select

    cast(0xD7DF6FC50A6807D31E27505A46DA7EA0 as bigint),

    cast(0xD7DF6FC50A6807D31E27505A46DA7EA0 as int),

    cast(0x1E27505A46DA7EA0 as int)

  • Actually, I think the following provides a better picture:

    select

    cast(0xD7DF6FC50A6807D31E27505A46DA7EA0 as bigint),

    cast(0xD7DF6FC50A6807D31E27505A46DA7EA0 as int),

    cast(0x1E27505A46DA7EA0 as bigint),

    cast(0x1E27505A46DA7EA0 as int),

    cast(0x46DA7EA0 as bigint),

    cast(0x46DA7EA0 as int)

  • Thanks Lynn..

    This helps 🙂

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply