Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Replacing a string when the value is found in a table Expand / Collapse
Author
Message
Posted Wednesday, May 1, 2013 11:29 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, May 2, 2013 3:19 PM
Points: 14, Visits: 47
Hi guys,

I'm trying to create a function where I can pass a string value that will be used for searching a table column and see if there is match, then replace that string partially when found like below:

Passed string: "John Doe University"

The column values from the table:

SELECT LastName FROM ThisTable
--------------------------------

LastName
---------
Cruz
Patar
Doe
QuiJong
Halal

So if the search is succesful, I'll replace the string with empty string, then the value will be returned as:

John University


Hope it makes sense?

Thanks
Post #1448692
Posted Thursday, May 2, 2013 1:09 AM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Monday, November 3, 2014 4:30 PM
Points: 4,574, Visits: 8,366
select @SearchString = REPLACE(@SearchString, LastName, '')
FROM ThisTable
select @SearchString
Post #1448706
Posted Thursday, May 2, 2013 1:45 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
Not exactly clear on what you are trying to accomplish, but my first instinct is to suggest the STUFF function. You can google it.

 
Post #1448717
Posted Thursday, May 2, 2013 3:20 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, May 2, 2013 3:19 PM
Points: 14, Visits: 47
Thanks guys, Sergiy's answer is exactly what I needed.

Have a nice day.
Post #1448993
Posted Friday, May 3, 2013 3:21 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 12:12 AM
Points: 1,953, Visits: 2,397
Sergiy (5/2/2013)
select @SearchString = REPLACE(@SearchString, LastName, '')
FROM ThisTable
select @SearchString

+1



_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1449097
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse