Casting and Data Type Conversion

  • Good and interesting question thanks

  • Nice question, Jason. Thanks.


    Peter MaloofServing Data

  • Thanks.. But still confused:-P.. need to work on it..

  • Mark Fitzgerald-331224 (7/15/2014)


    This is two's complement numbers being used.

    When converting down from varbinary to an integer data type you only get the last (least significant) byte. In this case the least significant bits are 1111 1110.

    In tinyint with would equate to the decimal values : 128,64,32,16,8,4,2 => 254

    With the signed SQL integer types then the most significant value is a negative of what you would expect. So for a smallint (2 bytes) the values are : -32768, 16384,8192,4096,2048,1024,512,256,128,64,32,16,8,4,2,1. In this format :

    BINARY -> DECIMAL

    0000000000000000 -> 0

    1000000000000000 -> -32768

    0111111111111111 -> 32767

    1111111111111111 -> -1

    http://en.wikipedia.org/wiki/Two's_complement

    Fitz

    Cool, thanks! I got this question wrong, so it's good to get more background on it.

    - webrunner

    -------------------
    A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
    Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html

  • Raghavendra Mudugal (7/15/2014)


    something happens in sql and it gives 254 as output.

    Nice 😀

    Thanks & Best Regards,
    Hany Helmy
    SQL Server Database Consultant

  • Jamsheer (7/15/2014)


    Thanks.. But still confused:-P.. need to work on it..

    Same confusion here, really. :w00t:

    Thanks & Best Regards,
    Hany Helmy
    SQL Server Database Consultant

  • BTY, @ the end of the explanation, it states that "For example, the following SELECT statement shows that the integer value 123456 is usually stored as a binary 0x0001e240", then there is no following select statement! 😉

    Thanks & Best Regards,
    Hany Helmy
    SQL Server Database Consultant

  • Hany Helmy (7/17/2014)


    Jamsheer (7/15/2014)


    Thanks.. But still confused:-P.. need to work on it..

    Same confusion here, really. :w00t:

    The problem is that tinyint is an unsigned datatype (unlike smallint, int, and bigint).

    Because it is unsigned, it doesn't know what to do with the value that is in the bit used for signs (positive/negative). Because there is a value in that bit, and the datatype is a tinyint, it just computes it as a bit value and continues on its merry way.

    Here is a decent article that discusses negative binary numbers.

    http://www.allaboutcircuits.com/vol_4/chpt_2/3.html

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Nice.Thanks for question.

    --rhythmk
    ------------------------------------------------------------------
    To post your question use below link

    https://www.sqlservercentral.com/articles/forum-etiquette-how-to-post-datacode-on-a-forum-to-get-the-best-help
    🙂

Viewing 9 posts - 16 through 23 (of 23 total)

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