Showing posts with label 4this. Show all posts
Showing posts with label 4this. Show all posts

Sunday, February 12, 2012

Bitwise AND in Where clause?

Is there any way to use Bitwise AND in a WHERE clause?
EG:
SELECT * FROM Directory WHERE DirectoryFeatured & 4
This just gives me an incorrect syntax error? DirectoryFeatured is an int
field.
Regards,
ChrisNot sure how you store the data, but possibly this is what you are looking f
or:
SELECT * FROM Directory WHERE DirectoryFeatured & 4 = 4
Consider normalizing the table and split each attribute to one column (or an
other table, depending
on the data etc).
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Chris Ashley" <chris.ashley@.SPAMblueyonder.co.uk> wrote in message news:42e64dc4.0@.entanet
..
> Is there any way to use Bitwise AND in a WHERE clause?
> EG:
> SELECT * FROM Directory WHERE DirectoryFeatured & 4
> This just gives me an incorrect syntax error? DirectoryFeatured is an int
field.
> Regards,
> Chris
>|||In SQL, WHERE clauses return one of TRUE, FALSE or UNKNOWN.
An t-SQL bitwise expression DirectoryFeatured & 4 returns an INT value which
is incompatible with the above set of values. A logical/bitwise AND between
the two expressions, taking each corresponding bit for both expressions.
Thus your expression should take the form of:
SELECT * FROM tbl WHERE col & 4 = <some val>
In general, there is not many instances when one needs to use bitwise
operators in commerical SQL applications. Though it occasionally finds its
place, quite often poorly designed schema is a prime reason for people
resorting to complex bitwise logic on integer typed columns.
Anith|||The statement is not complete. Try (untested):
SELECT * FROM Directory WHERE DirectoryFeatured & 4 = 4
The bitwise operator will return a value (not True or False); you have to
test for the expected value.
As an aside, this is a relatively inefficient thing to do, and it would
probably be best to create an encoding scheme that did not rely on low-level
bit manipulation.
"Chris Ashley" <chris.ashley@.SPAMblueyonder.co.uk> wrote in message
news:42e64dc4.0@.entanet...
> Is there any way to use Bitwise AND in a WHERE clause?
> EG:
> SELECT * FROM Directory WHERE DirectoryFeatured & 4
> This just gives me an incorrect syntax error? DirectoryFeatured is an int
> field.
> Regards,
> Chris
>