Somewhat related to my question about integers instead of decimals; my vendor provides a lot of "boolean" fields in the char(1) format (i.e. Y/N). Some of these fields legitimately cannot be a boolean because they can have several values, but the majority can be treated as a boolean. In my previous question the advice was to do what's "right" instead of what the vendor provides; should I still apply this logic and create my database schema using a bit field for these columns, or keep it as a char(1) and do the conversion in my code?
Also on this subject, how should I deal with the tri-state fields as far as code goes? Logically the field is a boolean (in the sense that I'm only interested in the Y/N value and the third value is really either yes or no), but the values can be more than just true/false (e.g. there is a UpsShippable
field that can be Y
, N
or G
); this field has multiple states so how would I best encapsulate it? Something like an enum (or static constants, since Enums can't be used with the char datatype)? In the multi-value cases the data is more like a type indicator than a flag.
To sum up (I get a little long-winded): 1) When dealing with char(1)
values in data, would you keep them as chars or convert to bit (or whatever your database's boolean type is) and why, and 2) How would you tackle a tri-state char field in your code, assuming you would leave it as char(1)
in the data schema?
EDIT: To clarify, none of these fields are used for "real" logic, it's basically just an indicator. For example, if the item isn't shippable via UPS (i.e. the value is N/G) then on the customer-facing page it says the item cannot be shipped via UPS, and on the back-end the logic won't make a call to UPS's web service to calculate shipping. The other Y/N fields are simply there as extra detail about an item and have no logic, although they need to be changeable (for example, have a checkbox indicating if it's recycled on a data entry form on the back end); I might display an image or filter items by them (e.g. you can search for all recycled products, and I'll check to make sure their recycled indicator is true) but nothing else, at least not at this point in time.