0

I'm trying to remove whitespace and trailing white space from right and left side of my string in DB.

Note how the current results looklike:

enter image description here

Note the string named:

*excellent-purchase*

When I fetch it in my C# application like this:

ctx.Users.ToList();

The output for this string that I get is:

\t*excellent-purchase*

I need to remove this "\t" sign from my C# application either on DB level or inside the C# application.

The way I've tried it is like doing it is like this:

UPDATE
    TableName
SET
    ColumnName = LTRIM(RTRIM(ColumnName))

But I still get this \t sign in my C# app...

How can I fix this?

Edit:

guys I still have a weird characther like this:

"nl_holyland*555*

And in the C# App it looks like:

\"nl_holyland*555*

Theres an extra \ with this solution like

User987
  • 3,663
  • 15
  • 54
  • 115
  • 2
    \t isn't whitespace it's a tab. use `Replace(ColumnName, char(9), '')` in addition. http://stackoverflow.com/questions/12906949/how-eliminate-the-tab-space-in-the-column-in-sql-server-2008 – Danny Cullen Jan 18 '17 at 15:22
  • You could also use [TrimStart](https://msdn.microsoft.com/en-us/library/system.string.trimstart(v=vs.110).aspx) – James Z Jan 18 '17 at 15:24
  • If there are other characters, you could look at [RegEx](http://stackoverflow.com/questions/1046740/how-can-i-validate-a-string-to-only-allow-alphanumeric-characters-in-it) – SQLMason Jan 18 '17 at 15:25
  • 2
    http://stackoverflow.com/questions/14211346/how-to-remove-white-space-characters-from-a-string-in-sql-server – Pரதீப் Jan 18 '17 at 15:30
  • 1
    Possible duplicate of [How to remove white space characters from a string in SQL Server](http://stackoverflow.com/questions/14211346/how-to-remove-white-space-characters-from-a-string-in-sql-server) – SQLMason Jan 18 '17 at 15:37

3 Answers3

1

You can try it:

string value= Regex.Replace(value, @"\t|\n|\r", "");

Junior Porfirio
  • 226
  • 1
  • 5
1

You might want to try the following:

UPDATE
    TableName
SET
    ColumnName = LTRIM(RTRIM(REPLACE(ColumnName,char(9),'')))
Tyron78
  • 4,117
  • 2
  • 17
  • 32
  • I still have a weird characther like this: "nl_holyland*555* And in the C# App it looks like: \"nl_holyland*555* Theres an extra \ with this solution like – User987 Jan 18 '17 at 15:42
  • This is most certainly because " is a reserved character and has to be escaped, which is done via adding \ to the beginning. – Tyron78 Jan 18 '17 at 16:19
1

You can use the Replace also:

UPDATE
    TableName
SET
    ColumnName =  REPLACE(ColumnName, ' ', '')
Afnan Ahmad
  • 2,492
  • 4
  • 24
  • 44