Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What Data_Type should I use.

Status
Not open for further replies.

Jimmy2128

Programmer
Feb 6, 2003
58
0
0
US
I want to add a field in my SQL table called YES/NO. Where the user have the choice. What Data_Type should I use.. Thanks for your help.
 
look at your copy of books online.

You can use any of the numeric data types available or the bit data type as being the "better" choices for that purpose.

you can also use any character data type.

It is your choice.

Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
Personal preference would be a bit datatype, though the problem with this type, is that it cant be included in an index.Use a Smallint if this will be an issue and make a constraint that values must be 1,0.


"I'm living so far beyond my income that we may almost be said to be living apart
 
if the values will always be Yes or No, On or Off, True or False... go with a bit data type, and make it non-nullable.

Regards,
John
 
If you have only 2 possible values for a field it doesn't matter (to the index) what data type it is since your selectivity is very low

unless..... you include this field as part of a covering index on the table, then make it a tiny int or char(1)



 
Hm... I just created index/did GROUP by on bit column.

?


------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
hmm, maybe fixed in a later version? my test was on sql server 2000 (see DDL in article linked to above)

r937.com | rudy.ca
 
oh, never mind the article i linked to, here is the DDL

create table test_bit
( id tinyint not null identity
, yesno bit not null )

insert into test_bit (yesno) values (1)
insert into test_bit (yesno) values (1)
insert into test_bit (yesno) values (0)
insert into test_bit (yesno) values (1)
insert into test_bit (yesno) values (0)

select * from test_bit

id yesno
---- -----
1 1
2 1
3 0
4 1
5 0

select yesno, count(*)
from test_bit
group
by yesno

Server: Msg 252, Level 16, State 2, Line 1
Cannot group by a bit column.



r937.com | rudy.ca
 
This works on my development machine
version is below
Microsoft SQL Server 2000 - 8.00.760 (Intel X86) Dec 17 2002 14:22:05 Copyright (c) 1988-2003 Microsoft Corporation Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 1)



 
No errors to me (SP4 Std edition).

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
well, what can i say

i don't have access to that machine no more, so i can't say what version sql server it was

live and learn, i guess

i will stick with TINYINT, eh :)

r937.com | rudy.ca
 
I can group by just fine on a bit column running SP4. You can not index a bit field however. In all honesty, creating a index on a bit field makes a whole lot of no sense because the index will be ignored anyway when the server does the execution plan. A bit field will always result in a table scan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top