Rolan
Rolan

Reputation: 3004

find exact string match and replace it in sql server

Is there a (supposedly) simple way to get an exact match when using the replace function in SQL Server 2012? (I'm open to other searching possibilities as well, of course)

For example, I'm using the following code to grab all the objects in a DB containing 'texter' in it at some point:

select OBJECT_NAME(object_id) name,
       OBJECT_DEFINITION(object_id) code,
       type
into #tester
from sys.objects
where OBJECT_DEFINITION(object_id) LIKE '%texter%'

This doesn't seem to differentiate between .texter, @texter or stupidtexter.

and so if I use:

update #tester 
set code = REPLACE(code, 'texter', 'bluenose')
where code LIKE '%texter%'

It's going to replace any variant of 'texter' with 'bluenose'

Let's assume I only want to replace the ' texter' and '.texter' versions of this and nothing else (noting that within each object it is possible that @texter or stupidtexter may also be present in the object code).

Is there a way I can differentiate between the variants of 'texter', @texter and stupidtexter?

Thanks

Upvotes: 2

Views: 7597

Answers (1)

Rolan
Rolan

Reputation: 3004

The solution was:

replace(REPLACE(code, '.texter', 'bluenose'), ' texter', 'bluenose')

Thanks to Sean Lange for the answer!

Upvotes: 1

Related Questions