Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty space at the end of SQL Server query results

I just installed Microsoft SQL Server 2008 R2 to test 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 that table and wrap each value with "'" I get the following:

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

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

SQL Server seems to be filling the unused space (because the column is a 20 byte nchar) with spaces.

What can I do to prevent this from happening?

like image 370
SuprDewd Avatar asked Aug 06 '10 21:08

SuprDewd


2 Answers

Use nvarchar instead of nchar. nchar is a fixed width field that is filled with spaces, as you're seeing.

like image 121
bobs Avatar answered Sep 28 '22 00:09

bobs


Change the column type to nvarchar.

See nchar and nvarchar.

like image 25
sgriffinusa Avatar answered Sep 28 '22 01:09

sgriffinusa