Should I use VARCHAR2 or CHAR as a datatype in Oracle?
It’s been suggested to me to use CHAR for these new tables I need but I’m concerned since these new tables which will be used to populat existing tables that use a VARCHAR2 datatype. I’m concerned about extra spaces being placed in the VARCHAR2 fields and with comparison issues. I know there are ways to compare them using by trimming or converting them but I’m afraid it will make my code messy and buggy.
What are your opinions?
It’s actually quite the opposite. Using CHAR will force your strings to be a fixed length by padding them with spaces if they’re too short. So when comparing CHARs to regular strings in whatever app is using the data, that app would need to add a trim every time. In other words, VARCHAR2 is the choice that naturally leads to cleaner code.
In general you should always use VARCHAR2, unless you have a very specific reason why you want a CHAR column.
If you’re worried about strings that have extra spaces in the front or end, then there’s a few options that come to mind: