Click here to Skip to main content
15,886,731 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
I want to remove '( City :- Mumbai )' from following string. In this City name is dynamically field it can be Jaipur, Delhi, Ajmer etc. using sql query.

The string is:-
'Address : Nehru Road Ext., Vile Parle (East)
Near Domestic Airport, Mumbai – 400099.
Maharashtra. INDIA. ( City :- Mumbai )
Tel : Not Available'

Plese help me as soon as possible
Posted
Updated 21-May-14 21:41pm
v2

REPLACE (T-SQL)[^] or SUBSTRING[^] together with PATINDEX[^] is what you're looking for.

The main idea is:
SQL
SELECT REPLACE(FieldName, SUBSTRING(FieldName, PStart, LEN(FieldName)-PEnd),'') AS NewFieldValues
FROM (
    SELECT FieldName, PATINDEX('%(City:%',FieldName) AS PStart, PATINDEX('%)%',FieldName) AS PEnd 
    FROM TableName
    WHERE PATINDEX('%(City:%',FieldName)>0
) AS T


You did not provide enough information to help you direct.

'Working' sample:
SQL
DECLARE @tmp TABLE (SomeString VARCHAR(255))

INSERT INTO @tmp (SomeString)
VALUES('Address : Nehru Road Ext., Vile Parle (East)
Near Domestic Airport, Mumbai – 400099.
Maharashtra. INDIA. ( City :- Mumbai )
Tel : Not Available'),
('Address : Bahru Road Ext., Vile Parle (West)
Near Domestic Airport, Calcuta – 422099.
Maharashtra. INDIA. ( City :- Calcuta )
Tel : UN')

SELECT REPLACE(SomeString, SUBSTRING(SomeString, PStart, PEnd-1-Pstart),'') AS NewFieldValues
FROM (
    SELECT SomeString, PATINDEX('%( City%',SomeString) AS PStart, PATINDEX('%Tel%',SomeString) AS PEnd 
    FROM @tmp
    WHERE PATINDEX('%( City%',SomeString)>0
) AS T

Result:
Address : Nehru Road Ext., Vile Parle (East)
Near Domestic Airport, Mumbai – 400099.
Maharashtra. INDIA. 
Tel : Not Available

Address : Bahru Road Ext., Vile Parle (West)
Near Domestic Airport, Calcuta – 422099.
Maharashtra. INDIA. 
Tel : UN
 
Share this answer
 
v4
Comments
CPallini 22-May-14 3:41am    
5.
Maciej Los 22-May-14 3:45am    
Thank you, Carlo ;)
 
Share this answer
 
Comments
Rakesh Tailor 22-May-14 3:22am    
City name is not static, it is dynamically.
This string in a column, I am getting from database with select query that time I want to replace these things.
Maciej Los 22-May-14 3:35am    
Please, see my answer!
Maciej Los 22-May-14 3:31am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900