White space at the end of SQL Server query results

I just installed Microsoft SQL Server 2008 R2 for testing with LINQ to SQL.

I have a table with one column of type nchar (20) and two rows: '123' and 'Test'.

If I query all the rows from this table and wrap each value with "", I get the following:

'123 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ' ( _ is a space) 'Test _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ' ( _ is a space) 

SQL Server seems to fill up unused space (because the column is a 20-byte nchar) with spaces.

What can I do to prevent this from happening?

+6
sql-server sql-server-2008 sqldatatypes
source share
3 answers

Use nvarchar instead of nchar. nchar is a fixed width field that is filled with spaces, as you can see.

+17
source share

Change the column type to nvarchar .

See nchar and nvarchar .

+2
source share

you need nvarchar , it is flexible.

+1
source share

All Articles