2

I have some columns using a bit datatype. The end user now wants an N/A option so what before I was using a bit to capture "Yes/No" (1 being yes and 0 being no) I now need to have three options Yes/No/NA which I can store just as text in a varchar.

Is there a way to change the datatypes of the columns from bit to varchar AND also update the current records data where any 0's I change to "No" and 1's I change to "Yes"?

4 Answers 4

8

Why not just use NULL to mean "n/a"?

Else use TINYINT with a lookup table containing values 0, 1, and 2 (or perhaps 1, 2, and 3) and their meanings. Then you add a FK between the two tables to enforce that only those values get entered.

The last choice would be to go with a CHAR(1) column. This would be ok only if you also specified a binary Collation, such as Latin1_General_100_BIN2. The binary Collation will make it so that you don't lose performance over the other two options. Without the binary Collation, the string column will compare values against linguistic rules that take extra time yet have no meaning for this usage. You would also need an AFTER INSERT, UPDATE Trigger to first UPPER() this value for consistency, and then to enforce that all values are either Y, N, or A (for "n/a"). This is more readable than 0, 1, and 2 (via TINYINT) and just as efficient for searching, and also just 1 byte, BUT uses will need to remember to specify upper-case letters only, else they might not get the expected results.

There is no reason at all to use VARCHAR(3) and store the full values N/A / Yes / No, unless you just don't care about the system getting slower ;-).

Sign up to request clarification or add additional context in comments.

3 Comments

+1000 to this. A bit datatype does not have 2 values, it has 3. 1, 0 and NULL.
Great idea and the simplest to implement since I can leave the DB alone and just change the code. Thanks!
@SeanLange Thanks :). Not sure if you ever messed with SilverLight development / XAML, but that was the only environment that I have seen where they offered a trinary boolean checkbox as a form element. You could actually check it, uncheck it, and then unset it! Unset would be a mostly shaded in checkbox, clearly different from the check mark. It was quite nice.
0
  1. Add a new varchar column
  2. update it based on the existing bit column
  3. remove the bit column
  4. (optionaly) rename the new column to the old column's name

Comments

0
Alter Table TableName
Alter Column ColumnName Varchar(3)

Update TableName
Set ColumnName = 'Yes'
Where ColumnName = '1'

Update TableName
Set ColumnName = 'No'
Where ColumnName = '0'

1 Comment

I'm not going to downvote, but I will point out that there is absolutely no benefit to making this change, and in fact, it will slow down queries against this field and require more space both on disk and in the buffer pool.
0

Actually char(3) would be more space efficient

or use tinyint with FK table as suggested by scutzly

I was dead was dead surprised but I can able to just convert bit to char(3) with right click design in SSMS.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.