Reputation: 15817
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.
Upvotes: 0
Views: 156
Reputation: 499382
The CHAR
data 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 larger CHAR
fields).
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
(or NVARCHAR
), not CHAR
.
Upvotes: 3
Reputation: 12804
This is not a bug. CHAR in SQL is a fixed length and pads with spaces to the length of the column definition. By "dot net char variable", I assume you are talking about a string datatype and not an actual array of chars.
If you want variable length strings without padding, then use VARCHAR or NVARCHAR.
Upvotes: 2