Posted By

rengber on 09/09/07


Tagged

sql tsql bitwise BitMask


Versions (?)

Using a BitMask in a Where Clause


 / Published in: SQL
 

  1. BEGIN
  2. DECLARE @MailDelivery int,
  3. @EmailDelivery int,
  4. @FaxDelivery int,
  5. @ParamVal int,
  6. @ZeroVal int
  7.  
  8. SET @MailDelivery = 1 --(0001)
  9. SET @EmailDelivery = 2 --(0010)
  10. SET @FaxDelivery = 4 --(0100)
  11.  
  12. SET @ParamVal = 1
  13. SET @ZeroVal = 0
  14.  
  15. IF @ParamVal = 0
  16. SET @ZeroVal = NULL
  17.  
  18. SET @ParamVal = isnull(@ParamVal, 0)
  19.  
  20. SELECT Id,
  21. --BusinessEventDate,
  22. DeliveryFlags,
  23. (isnull(DeliveryFlags,0) & @ParamVal) AS BitMask
  24. FROM Mailsets
  25. WHERE (BusinessEventDate > '8/28/2007')
  26. --Check the appropriate bit.
  27. AND (
  28. (isnull(DeliveryFlags,0) & @ParamVal) = @ParamVal + isnull(@ZeroVal, isnull(DeliveryFlags,0))
  29. )
  30.  
  31. END
  32. GO

Report this snippet  

You need to login to post a comment.