Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / Programming / SQL / July 2008

Tip: Looking for answers? Try searching our database.

BIT data type confusion

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mscertified - 25 Jul 2008 22:35 GMT
According to help this can have value of 0, 1 or null

Why then when I display the select results do I see value of -1?
And in Management Studio table display I see True or False

Also, help says I can refer to 0 or 1 as FALSE or TRUE yet I dont see these
values in the reserved words.
Can I say
Declare MyBit BIT
If MyBit = TRUE
Aaron Bertrand [SQL Server MVP] - 25 Jul 2008 23:05 GMT
> Why then when I display the select results do I see value of -1?

Display the select results WHERE?  In Access?  VB?  This is likely due to
the formatting of the client tool you are using.  Try running the select in
a query window in Management Studio (not "open table").

> And in Management Studio table display I see True or False

Yes, this was a bonehead-ed decision by someone who mistook BIT for BOOLEAN
and programmed the grid using the .NET data type instead of understanding
what he/she was actually doing.  See these URLs:

connect.microsoft.com/SQL/feedback/ViewFeedback.aspx?FeedbackID=124584

connect.microsoft.com/SQL/feedback/ViewFeedback.aspx?FeedbackID=264592

connect.microsoft.com/SQL/feedback/ViewFeedback.aspx?FeedbackID=330293

Sadly, this problem is identical in SQL Server 2008.  :-(

> Also, help says I can refer to 0 or 1 as FALSE or TRUE yet I dont see these
> values in the reserved words.

What help?  Be specific.  If there is help around the management studio grid
(e.g. Open table), then it means you can use TRUE or FALSE when typing in
data as a string (this is not the same as a keyword or reserved word).  Did
the help mention that open table does not accept 1 or 0?  It doesn't.  And
did it show any examples of using TRUE or FALSE in a query (as keywords, not
as strings)?  It couldn't have, because

> Can I say
> Declare MyBit BIT
> If MyBit = TRUE

No, did you try it?  When I do this:

DECLARE @MyBit BIT;
SET @MyBit = TRUE;

I get msg 207, "Invalid column name 'TRUE'."

You can, for some reason, do this against 2005 or better, but it yields msg
245 in 2000:

DECLARE @MyBit BIT;
SET @MyBit = 'FALSE';

(Note that 'FALSE' is a string, not a keyword.)

For true, you can also use 'TRUE' or 'tRuE' or 'true' or -1 or 1 or 6 or e
or pi or your weight or your age or your phone number or your birth year or
ridiculously large numbers, e.g.

DECLARE @MyBit BIT;
SET @MyBit = -21271827182718271.87871827182; -- <= 38 significant digits
SELECT @MyBit;

My suggestion: stay away from open table.  And always use 0 or 1 for BIT
values.  Modify your data in a query window.  This is not a spreadsheet and
sadly Microsoft has tried to make the distinction blurry by encouraging this
kind of behavior and messing it up in the process.  My guess is that someday
they will fix this ungodly mess and either add BOOLEAN or fix the way BIT is
handled or both.  Some things will need to remain for backward compatibility
but there is plenty of room for improvement.
steve dassin - 26 Jul 2008 00:57 GMT
Laser focused. Awesome  :-)

>> Why then when I display the select results do I see value of -1?
>
[quoted text clipped - 73 lines]
> compatibility
> but there is plenty of room for improvement.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.