I remember a long time ago when I was working with characters in .NET. The value taken from the SQL database (char field) was stored in a dot net char variable as: “a ” (a series of blank spaces after the character). This bug in my application caused problems.
Can anyone explain when this happens as I cannot seem to recreate the behaviour and I want to avoid bugs in the application I am working on.
The
CHARdata type in SQL Server is a fixed size field.So for
CHAR(2), if you only populate it with a single character, a space will be added as padding (and more spaces for largerCHARfields).When read from the DB, you will get these spaces as well. This is not an error.
You should be using the correct data type for your database fields – if you have variable length text, use
VARCHAR(orNVARCHAR), notCHAR.