jeudi 9 juin 2016

How to replace a character in a string using T-SQL

In my table column, i have below sample data

Test1 145,  Area 1
Test2 146,
Test3 145,  Area 2, Plot 10

What i want to achieve is to replace "," in the string but only if it is the last character. If i have more characters after "," then the replace should leave the string as it is.

In the example above, the replace would only work in line 2.

The expected out put would be like below

Test1 145,  Area 1
Test2 146 
Test3 145,  Area 2, Plot 10

In line 2 above, "," has been replaced with empty space.

I have tried this Replace(column1, ', ', '') AS ColName but this replaces the "," in Test1 and Test3.

Aucun commentaire:

Enregistrer un commentaire