views:

109

answers:

6
+4  Q: 

varchar(1) or bit?

Possible Duplicate:
NCHAR(1) vs BIT

I have a field in the database that currently stores two values: 'Y' and 'N' (yes, no). The field datatype = varchar(1). Would it be better to convert 'Y' to 1 and 'N' to 0 and to use a bit datatype? Is there any benefit to using varchar(1) over bit in this case?

+2  A: 

Scalability, it might of originally been designed with future thought of having a maybe option, or other states.

Fundamentally there really is very little difference, but I always tend to go for bit values for boolean data types as it keeps data integrity (don't have to worry about upper/lower case Y/N values).

Also don't change it if you don't need to, it can often cause more problems down the line that you wont expect. It wont provide any real benefit to change it, you'd just spend time checking it all works for no real benefit to the end user.

If you are designing a system though, use bit.

Tom Gullen
+10  A: 

Use BIT, it will express the true meaning of the field much better (a boolean, or on/off value).

You also won't have to worry about string case, people storing bad values, people mis-understanding what the field is supposed to be used for, etc.

Justin Niessner
Any benefit for using varchar(1) in this case?
subt13
Unless you're planning on adding values other than Y or N later down the line (which I would strongly recommend you NOT do), then no.
Justin Niessner
+3  A: 

Use a bit. Your app code should decide what to display in place of this in whatever you're using for a UI.

David Lively
A: 

If there is any difference it will be so minimal I doubt you'll ever notice it. I like 1 & 0 personally, but that's just me. Technically, NVARCHAR(1) takes up more space than Bit (a byte vs a bit), but I doubt it would matter.

AllenG
+5  A: 

use bit because SQL Server will use only 1 byte to store 8 of these columns in a row. and you don't have to add a constraint to only allow 1 and 0

Also don't use varchar(1) to store 1 character, use char(1) if you have to. Varchar will use 2 extra bytes to tell sql server where the position starts and ends for a varchar data type, char doesn't have to do this since it is a fixed size

SQLMenace
Good explanation for varchar vs char +1
subt13
+1  A: 

Use Bit:

Logical representation - since boolean representations aren't always Y/N, which would then mean you would either need to be inconsistent, or non-intuitive. e.g. True/False (T/F), On/Off, Open/Closed etc etc.

Referential integrity - non-nullable bit can only be 0 or 1. Unless you add constraints, your varchar could be Y,N, X

In theory Bits can be packed, so would also smaller storage.

nonnb
Yes, I've seen other values sneak in like 'X' before. Good point.
subt13