Tuesday, March 27, 2012
Boolean ?
Most people use a BIT (0 or 1).
There are arguments for and against using CHAR(1) values with a check
constraint ('Y', 'N'), ('T', 'F').
You're better off with either of those than a (var)char(5) allowing 'TRUE'
and 'FALSE' as strings.
Oh, and you will never be able to shorthand WHERE column = 1 for WHERE
column, or WHERE column = 0 for WHERE NOT column. Even though the datatype
you choose (along with the cnstraints if necessary) may seem like a valid
boolean expression to you, SQL Server does not consider them as such.
A
"Gabriel" <nospam@.nospam.com> wrote in message
news:uw2bPk2$GHA.3380@.TK2MSFTNGP04.phx.gbl...
> Hello,
> Is there a way to define a boolean with SQL Server 2005 (Express)
> Thanks,
>
>> Btw, did you see the new error message 2005 gives you when you try to say[vbcol=seagreen]
Hehe, apparently there is some context that expects boolean type in SQL
Server !!!
Anith
|||>>> Btw, did you see the new error message 2005 gives you when you try to
> Hehe, apparently there is some context that expects boolean type in SQL
> Server !!!
Well, there are boolean expressions (not types), sort of, e.g.
WHERE EXISTS
(
...
)
|||Sorry, just wanted to clarify that I do get the joke. It's been a long day
here today.
|||>> It's been a long day here today.
Long Fridays, tell me about it :-)
Anith
Sunday, February 12, 2012
bitwise AND with Char column
category (int, smallint, tinyint). but i have a char(4) column which
stores integers (disregard the db design issue) which produces the
same/correct results after using bitwise AND with another integer.
does this mean SQL server does the conversion itself?SQL Server will convert a value stored in the CHAR datatype into an INTEGER
because an INTEGER has a high
Data Type Precedence
"joeycalisay" <joeycalisay@.gmail.com> wrote in message
news:1131435508.269803.57870@.o13g2000cwo.googlegroups.com...
> based on BOL, bitwise AND operator operates only on integer data type
> category (int, smallint, tinyint). but i have a char(4) column which
> stores integers (disregard the db design issue) which produces the
> same/correct results after using bitwise AND with another integer.
> does this mean SQL server does the conversion itself?
>|||thanks a lot sir!|||Implicit conversion is at work here and that's potentially bad news.
Implicit conversions are to be avoided where possible because of their
potential to break unexpectedly under schema or code changes or even as
a result of service packs or version upgrades. Obviously a poor design
is at fault in your example but if you must accommodate it then I would
use CASE expressions:
CASE WHEN col1 = '1' AND col2 = '1' THEN 1 ELSE 0 END
David Portas
SQL Server MVP
--|||thanks for the input sir david particularly on the point of it being
breakable. poor design - yeah, i hated it when i saw it, wish i had
the time to revise it but they are at code freeze
Friday, February 10, 2012
Bit or Char?
Generally, unless you have a number of bits in a tables, either would be OK. I often use Y/N and haven't had issues yet. Have a look at the following.
http://www.codecomments.com/archive352-2005-11-712750.html
|||There is a major difference when you are using BIT because it is Boolean in SQL Server and from SQL Server 7.0 and above BIT is three valued in SQL Server because Boolean in ANSI SQL is three valued True/False/NULL(unknown). So you cannot compare the use of BIT to Char in SQL Server 7.0 and above. Hope this helps.|||If the value you are storing is truly a true/false or yes/no, then use a bit.
Bits are smaller (1/8th of a byte, and when combined with multiple bits in a row, this can add up significantly).
They are also faster (Although probably insignificantly faster).
Using a bit also restricts the values to 0 or 1 (or null if nullable).
Let's assume that you have a table in which you are storing whether an item has been sold. The column is named IsSold, and you want to mark a particular item as being sold. What do you put in the IsSold column?
What if it is defined as a bit? The answer is 1.
What if it is defined as char? Hmm...do I put '1'? Maybe 'Y', or 'T', oh... Maybe 'y' or 't'? What happens if I put in 'Q'?
|||For boolean-type values I always prefer to use Bit datatype. One advantage is that you can directly convert an SqlServer bit datatype to a .NET Boolean using Convert.ToBoolean()
eg.
Boolean isApproved = Convert.ToBoolean(myReader["IsApproved"]);
Obviously you have to make sure your column has a not null constraint for this to be guaranteed to work!|||
Motley wrote:
If the value you are storing is truly a true/false or yes/no, then use a bit.
Bits are smaller (1/8th of a byte, and when combined with multiple bits in a row, this can add up significantly).
They are also faster (Although probably insignificantly faster).
Using a bit also restricts the values to 0 or 1 (or null if nullable).
Let's assume that you have a table in which you are storing whether an item has been sold. The column is named IsSold, and you want to mark a particular item as being sold. What do you put in the IsSold column?
What if it is defined as a bit? The answer is 1.
What if it is defined as char? Hmm...do I put '1'? Maybe 'Y', or 'T', oh... Maybe 'y' or 't'? What happens if I put in 'Q'?
No BIT is Microsoft proprietry implementation of ANSI SQL Boolean but it is defined as INT which means it is four bytes. This is because Boolean in ANSI SQL is three valued so ANSI SQL will not define a data type for it. Hope this helps.
|||If i use a Y/N field, I set a rule that these are the only options.
Another valid point is that Y or N might more closely relate to the business data definition, and be more welcome by business data users who might have to work closely with the database.
Most databases are set up (the default) as case-independant, so case is not usually an issue - but if have already set Y or N in the rule - it's covered.
There's no right or wrong here, its all a matter of taste
|||If Binary Sort is enabled SQL Server must be case sensitive. Hope this helps.|||
Caddre wrote:
Motley wrote:
If the value you are storing is truly a true/false or yes/no, then use a bit.
Bits are smaller (1/8th of a byte, and when combined with multiple bits in a row, this can add up significantly).
They are also faster (Although probably insignificantly faster).
Using a bit also restricts the values to 0 or 1 (or null if nullable).
Let's assume that you have a table in which you are storing whether an item has been sold. The column is named IsSold, and you want to mark a particular item as being sold. What do you put in the IsSold column?
What if it is defined as a bit? The answer is 1.
What if it is defined as char? Hmm...do I put '1'? Maybe 'Y', or 'T', oh... Maybe 'y' or 't'? What happens if I put in 'Q'?
No BIT is Microsoft proprietry implementation of ANSI SQL Boolean but it is defined as INT which means it is four bytes. This is because Boolean in ANSI SQL is three valued so ANSI SQL will not define a data type for it. Hope this helps.
1. A proprietor.2. A group of proprietors.3. Ownership; proprietorship.4. A proprietary medicine.5. One granted ownership of a proprietary colony.
No, it is not.
ISO/ANSI defined the bit datatype in SQL-99. See ISO/IEC document 9075-1:1999.
The majority of the large RDMSs support the bit datatype. Off the top of my head, that'd be MSSQL, MySQL, Postgress, Mimer, Frontbase, and SyBase.
Maybe you think the word "proprietry" (not spelled correctly by the way) means cool or useful, in that case I'd agree.
|||EDIT
Try the link below for answer from Joe Celko about BIT and Joe Celko is a member of ANSI SQL and wrote to date the best ANSI SQL book. And BIT in SQL Server is not a data type because it is defined as INT. There is no data type defined for Boolean in ANSI SQL because it is three valued and I have ANSI SQL 2003.
http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=9&messageid=213719#bm214191
Sorry permissions required to access the link, the text below was posted by Joe Celko.
(There is no Boolean data type in SQL because of the 3VL. BIT is a horrible, proprietary, non-relational thing that you should avoid. Instead define a location type code that can hold all of the various kinds of locations you will need. Make 0= "fiscal" in the code.
CREATE TABLE ClientLocations
(location_id
REFERENCES Locations(location_id)
ON UPDATE CASCADE
ON DELETE CASCADE,
client_id INTEGER NOT NULL
REFERENCES Clients(client_id)
ON UPDATE CASCADE
ON DELETE CASCADE,
location_type INTEGER DEFAULT 0 NOT NULL
CHECK (location_type >= 0),
PRIMARY KEY (location_id, client_id, location_type));)
|||
I think I'm done here. There really isn't anything else I can do but keep repeating what I have already stated.
BIT is not defined as INT - As proven, and provable by anyone else here with MS SQL 2000. BIT fields do not take 4 bytes of data storage as a INT would. They act like I said.
The BIT datatype is defined in SQL-99, however you are correct, it appears to be removed in SQL-2003.
The BIT datatype is not proprietary, and your (or others) claiming it is, simply does not make it so.
Just on a side note, Joe Celko may have written a good book, I don't know, I didn't read it, however, it is apparent that he tends to make claims that are based on his opinion and state them as fact when they are not facts. A bit is not a "horrible, proprietary, non-relational thing". That statement is absurd. A bit is more relational, and more normalized than a CHAR field with checks. And I've already disproven the proprietary part, so all we are left with is the truth. Joe thinks bits are horrible. Great, that's his OPINION, one that most people well versed in database design, structure, and standardization would disagree with. However, it is just an opinion.
|||If you are through with the thread you will not post again and your opinion compared to one of the most brilliant minds in ANSI SQL?
( A bit is more relational, and more normalized than a CHAR field with checks)
(So you cannot compare the use of BIT to Char in SQL Server 7.0 and above)
I covered that a long time ago.