Search code examples
c#.netsql-serverazureazure-sql-database

SQL Server Retrieve data out, the data got extra many space


I am using Azure SQL Server database. However, when I retrieve data from SQL Server, the data got a lot of extra spaces. I don't know how, did the client enter any space in my data? Is there anyway for me to not select those extra spaces from my database?

enter image description here


Solution

  • Because your column type is nchar(50), it will always contain 50 chars no matter the length of data inserted. The remaining chars are padded with whitespace.

    To fix this you have to change the table definition so that the email column is nvarchar(50), and then you can update the table removing all the whitespace.

    It's probably also worth doing what some of the other answers have suggested and trim additional whitespace in your app, because there is still nothing preventing an insert with additional whitespace.