locked
Trailing Spaces RRS feed

  • Question

  • Hi

    I have a pretty simple table setup with 3 columns-

    ID (PK)

    SettingName (nvarchar(100))

    SettingValue (nvarchar(100))

    The data in SettingValue column varies in length. When i first add the data into the table manually, the text length is exactly that but once i have read the data into a form and then saved back, the text field is much longer which i suspect is to match the length of 100. Is there a way to trim it down in the database so the the length is the real length, not a static length of 100?

    I can trim it down when i read single values but when i attach the table as a datasource to a gridview, the grid rows are full of space..

    Thanks


    I am here to ask questions and learn from others. Cor Ligthert DO NOT REPLY TO ANY OF MY POSTS!!

    Tuesday, August 4, 2020 8:17 AM

Answers

  • SQL Server do not add trailing spaces to a column of type "varchar", that must be caused by your application.

    Olaf Helper

    [ Blog] [ Xing] [ MVP]

    • Marked as answer by NachoShaw Tuesday, August 4, 2020 10:21 PM
    Tuesday, August 4, 2020 9:14 AM
    Answerer

All replies

  • SQL Server do not add trailing spaces to a column of type "varchar", that must be caused by your application.

    Olaf Helper

    [ Blog] [ Xing] [ MVP]

    • Marked as answer by NachoShaw Tuesday, August 4, 2020 10:21 PM
    Tuesday, August 4, 2020 9:14 AM
    Answerer
  • You can say

    UPDATE tbl
    SET    col = rtrim(col)

    to remove the trailing spaces.

    But you should figure out why you got all thoses space there in the first place. (But that is more an issue for the client environment you are using than an SQL Server question.)


    Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

    Tuesday, August 4, 2020 9:17 AM
  • Hey

    Thanks for the replies. I managed to resolve it. In my Update Stored Procedure, the parameters were set as nchar(100) instead of varchar(100)

    Thanks for pointing me in the right direction

    Nacho


    I am here to ask questions and learn from others. Cor Ligthert DO NOT REPLY TO ANY OF MY POSTS!!

    Tuesday, August 4, 2020 10:20 PM