Search code examples
sql-serversql-server-2000whitespace

How to remove white spaces from columns in SQL Server 2000?


I have an old SQL Server 2000 database from which I read data. My problem is every query involving a String returns a value the size of that column filled with blank spaces.

e.g: let's say we have a column called NAME CHAR(20). All queries would return:

"John                    " 

instead of just "John".

Is there a configuration or parameter in my database that causes this, or anything at all that can be changed to avoid it? Thank you.

EDIT:

I'd like to clarify, I'm reading my DB using JPA Repositories. I don't want to physically remove the whitespaces from the columns, or trim the values manually using RTRIM/LTRIM/REPLACE. I'm just trying to retrieve the column without trailing spaces, without adding any extra strain to the query or trimming the fields programatically.


Solution

  • you can use REPLACE/RTRIM/LTRIM

    select RTRIM(LTRIM(column_name)) from table_name
    

    or

    select  replace(column_name, ' ', '') from table_name