Hi,
I want to do a logical comparison between two tinyint-columns in a select
statement.
With logical I mean: if a bit in the tinyints are both 1 then comparison is
true.
00001000 and 11000001 = false
10101010 and 00000010=true
Can this be done in SQLServer?
Thanks
FrankThe bitwise AND logical operator in SQL Server is '&'. This expects integer
values. Converting your binary values to integer:
IF 8 & 193 = 193 PRINT 'true' ELSE PRINT 'false'
IF 170 & 2 = 2 PRINT 'true' ELSE PRINT 'false'
See Bitwize AND in the Books Online for more information.
Hope this helps.
Dan Guzman
SQL Server MVP
"Frank" <frank@.frank.com> wrote in message
news:41e50fbc$0$6211$e4fe514c@.news.xs4all.nl...
> Hi,
> I want to do a logical comparison between two tinyint-columns in a select
> statement.
> With logical I mean: if a bit in the tinyints are both 1 then comparison
> is true.
> 00001000 and 11000001 = false
> 10101010 and 00000010=true
> Can this be done in SQLServer?
> Thanks
> Frank
>|||GREAT!!!!!
Frank
"Dan Guzman" <guzmanda@.nospam-online.sbcglobal.net> schreef in bericht
news:uaWOg4K%23EHA.1296@.TK2MSFTNGP10.phx.gbl...
> The bitwise AND logical operator in SQL Server is '&'. This expects
> integer values. Converting your binary values to integer:
> IF 8 & 193 = 193 PRINT 'true' ELSE PRINT 'false'
> IF 170 & 2 = 2 PRINT 'true' ELSE PRINT 'false'
> See Bitwize AND in the Books Online for more information.
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "Frank" <frank@.frank.com> wrote in message
> news:41e50fbc$0$6211$e4fe514c@.news.xs4all.nl...
>|||Frank,
Dan pointed you in the right direction, and I'll just note that
to see if X and Y have a common bit set, compare X & Y
with 0.
Steve Kass
Drew University
Frank wrote:
>Hi,
>I want to do a logical comparison between two tinyint-columns in a select
>statement.
>With logical I mean: if a bit in the tinyints are both 1 then comparison is
>true.
>00001000 and 11000001 = false
>10101010 and 00000010=true
>Can this be done in SQLServer?
>Thanks
>Frank
>
>|||"Steve Kass" <skass@.drew.edu> wrote in message
news:uB%23xjUL%23EHA.1188@.tk2msftngp13.phx.gbl...
> Frank,
> Dan pointed you in the right direction, and I'll just note that
> to see if X and Y have a common bit set, compare X & Y
> with 0.
> Steve Kass
> Drew University
>
If X & Y = 0 then you have no bits in common.
X 1001
Y 0110
======
0000
IF X & Y = Y then you have Y bits in common.
X 1001
Y 0001
======
0001 <-- Same as Y
IF X & Y > 0 then you have at lest some bits in common, but not necessarily
all of them.
X 1001
Y 0011
======
0001 > 0 At least one bit in common.
Rick Sawtell
MCT, MCSD, MCDBA
Friday, February 24, 2012
logical and
Labels:
bit,
comparison,
database,
logical,
microsoft,
mysql,
oracle,
selectstatement,
server,
sql,
tinyint-columns,
tinyints
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment