Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Replace Words in Record

Author  Topic 

brendanb
Starting Member

7 Posts

Posted - 2013-10-16 : 19:11:51
Hi,

Just after some ideas on how this could be done

For Example, I have the following string of words in a field

'COOKTOP WITH CAST IRON TRIVETS'

I would like to be able to split this string into its words then be able to use a dictionary to replace some of the words with new words.

So I would have a table/dictionary like

Source_Word | Replace_Word
IRON | Metal

Updated String

'COOKTOP WITH CAST METAL TRIVETS'

How could this be achieved?

brendan

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-10-17 : 04:28:55
isnt it just a matter of this?


declare @str varchar(5000) = 'This is a test sentence to test the replacement of word based on table'

declare @t table
(
strval varchar(100),
replaceval varchar(100)
)
insert @t
values('test','sample'),('word','part')

select @str = REPLACE(' ' + @str + ' ',' ' + strval + ' ',' ' + replaceval + ' ')
from @t
WHERE @str LIKE '% ' + strval + ' %'

select @str


output
------------------------
This is a sample sentence to sample the replacement of part based on table



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -