Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove certain characters from a string

I'm trying to remove certain characters.

At the moment I have output like cityname district but I want to remove cityname.

SELECT Ort FROM dbo.tblOrtsteileGeo WHERE GKZ = '06440004' 

Output:

Büdingen Aulendiebach Büdingen Büches Büdingen Calbach Büdingen Diebach Büdingen Dudenrod Büdingen Düdelsheim 

Desired output:

Aulendiebach Büches Calbach Diebach Dudenrod Düdelsheim 
like image 874
UrKll Avatar asked Jan 14 '13 13:01

UrKll


People also ask

How do I remove a few character from a string in Python?

You can remove a character from a Python string using replace() or translate(). Both these methods replace a character or string with a given value. If an empty string is specified, the character or string you select is removed from the string without a replacement.

How do I remove a specific item from a string in Python?

Using translate(): translate() is another method that can be used to remove a character from a string in Python. translate() returns a string after removing the values passed in the table. Also, remember that to remove a character from a string using translate() you have to replace it with None and not "" .

How do you delete a certain character in a string Java?

The idea is to use the deleteCharAt() method of StringBuilder class to remove first and the last character of a string. The deleteCharAt() method accepts a parameter as an index of the character you want to remove.

How do I remove a specific character from a string in SQL?

The TRIM() function removes the space character OR other specified characters from the start or end of a string. By default, the TRIM() function removes leading and trailing spaces from a string.


1 Answers

You can use Replace function as;

REPLACE ('Your String with cityname here', 'cityname', 'xyz') --Results 'Your String with xyz here' 

If you apply this to a table column where stringColumnName, cityName both are columns of YourTable

SELECT REPLACE(stringColumnName, cityName, '') FROM YourTable 

Or if you want to remove 'cityName' string from out put of a column then

SELECT REPLACE(stringColumnName, 'cityName', '') FROM yourTable 

EDIT: Since you have given more details now, REPLACE function is not the best method to sort your problem. Following is another way of doing it. Also @MartinSmith has given a good answer. Now you have the choice to select again.

SELECT RIGHT (O.Ort, LEN(O.Ort) - LEN(C.CityName)-1) As WithoutCityName FROM   tblOrtsteileGeo O        JOIN dbo.Cities C          ON C.foo = O.foo WHERE  O.GKZ = '06440004' 
like image 115
Kaf Avatar answered Sep 21 '22 10:09

Kaf