I am building a ASP.NET MVC application with Entity Framework and SQL Server.
I have noticed that when I read back SQL Server columns of type nchar
or text
there will be added blanks at the end of the string. To fix this I have to run Trim() on the string?
How can I solve this?
Best Regards
Advertisement
Answer
I have notice that when I read back MS
SQL columns of type nchar
NCHAR
is fixed length. If an INSERT
ed or UPDATE
ed value has a smaller length, then spaces are added. Naturally you read back with additional blanks.
This happens when the database is set up in such a way to add blanks.
if you do not want fixed length strings, don’t use a fixed length field type. Instead of NCHAR
use NVARCHAR
. The VAR
in there indicates variable length.