Doubt in Understanding in Where condition

  • Hi All

    I have got a table " with column id and CD

    ID CD

    ---- ----

    1A

    2B

    3C

    4D

    5E

    6F

    7G

    8H

    Please help me understand

    when I write a query

    "SELECT * FROM TRIAL WHERE ID &3 > 1 "

    i get the result as

    ID CD

    ---- ----

    2B

    3C

    6F

    7G

    what is the logic behind it ?

  • ID & 3 is doing bitwise manpulation

    3 has the bit pattern (just using 1 byte) 00000011

    2 has the bit pattern 00000010.

    When you do a logical and of 3 and 2 the following

    00000010

    AND

    00000011

    Gives

    00000010

    As you do an AND of each bit. Convert the result back to decimal and it gives 2, which is greater than 1, hence returned by yout query.

    Now, let's try 5

    5 has the bit pattern 00000101

    00000101

    AND

    00000011

    Gives

    00000001

    Convert that back to decimal and it's 1. You're filtering for >1, hence the row is not returned.

    Does that help?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Thanks a lot Gail Shaw.

    It makes more logic now.

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

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