sql-serversql-types

How to get so the rows in a table doesn't get filled with unneeded spaces to fill out everything


I have a table which fills out the rows I have put in there with spaces if the thing I put in there isn't long enough.

For example:
I have a string called

'ABC'

but the column is a nchar(10) so the value which is put in there becomes

'ABC       '

Does anyone know what the problem could be?

I am working in MSSQL.


Solution

  • nchar is a fixed width datatype and will always be padded out with spaces up to the defined column limit. You would need nvarchar(10) to avoid this.

    You should generally only use nchar in preference to nvarchar when the values in the column all have the same or very similar lengths.