Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Other SQL Server Topics (2005)
 Help with understanding part of a query

Author  Topic 

Kwozzie
Starting Member

4 Posts

Posted - 2012-04-15 : 23:25:26
Hi,

I've been asked to provide extra reports for a program that uses an MSSQL server. I have some experience with MySQL so understand a fair bit of the syntax but I'm stumped by a part of a query and would like some help understanding what the (Flags & 2) part of the query is trying to achieve.


Where CreditLimit > 0 And (Flags & 2) = 0
--And (Flags & 8) = 0
--And (Flags & 128) = 0


The Flags field is an int and allows NULLs. I'm not sure what other information is needed?
I don't have any background knowledge of the structure of this db, nor any reference for Flags intended purpose. Am I on a wild goose chase without this info?

I ran a query on just Flags so there is an idea of the data in there:
SELECT TOP 30 Flags FROM [tableinquestion] GROUP BY Flags ORDER BY Flags


Results:
-----------
NULL
0
4
5
6
7
8
9
10
12
13
14
15
72
78
128
132
133
134
135
136
138
140
141
142
143
196
198
200
202

Any ideas/suggestions?

Kwozzie
Starting Member

4 Posts

Posted - 2012-04-16 : 00:50:30
NM. I understand what it's doing, still no idea what it's for, a question for the developer of the program if I were allowed to ask.
[url]http://msdn.microsoft.com/en-US/library/ms174965%28v=sql.90%29.aspx[/url] and [url]http://en.wikipedia.org/wiki/Bitwise_operations_in_C[/url] should help those looking for similar answers.

It also helped me understand what's happening by doing another query to show the outcomes...
SELECT Flags, (Flags & 2) as Flags_2, (Flags & 8) as Flags_8, (Flags & 128) as Flags_128 FROM [tableinquestion] ORDER BY Flags ASC
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-16 : 12:15:30
i think Flags is field which indicates the status of multiple things based on bits that are set inside it. By doing an & 2,& 8 etc you're trying to check whether a particular bit is set or reset which will determine some condition as per your business logic

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -