SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Other SQL Server Topics (2005)
 Help with understanding part of a query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Kwozzie
Starting Member

Australia
4 Posts

Posted - 04/15/2012 :  23:25:26  Show Profile  Reply with Quote
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

Australia
4 Posts

Posted - 04/16/2012 :  00:50:30  Show Profile  Reply with Quote
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.
http://msdn.microsoft.com/en-US/library/ms174965%28v=sql.90%29.aspx and http://en.wikipedia.org/wiki/Bitwise_operations_in_C 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

India
47042 Posts

Posted - 04/16/2012 :  12:15:30  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000