用另一个表中的词替换句子中的词 [英] Replace words in a sentence with words from another table

查看:29
本文介绍了用另一个表中的词替换句子中的词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SQL Server,我想创建一个 SELECT 查询,用另一个表中使用的词替换字符串中的一个或多个词.像这样:

I'm using SQL Server and I want to create a SELECT query that replaces one or more words in a string with words used in another table. Like this:

SELECT [Message] from Table1

返回:

Hello, my name is Thomas and i'm from Belium.

Table2 我有两列

Original_Word-------Replace_Word
is------------------------is not
i'm-------------------------i am

所以我需要的选择查询必须返回:

So the select query I need must return this:

Hello, My name is not Thomas and i am from Belgium

有人可以帮忙吗?

推荐答案

可以使用动态sql构建嵌套替换:

You can use dynamic sql to build a nested replace:

DECLARE @sql varchar(max) = ''' '' + [Message] + '' ''';

SELECT @sql = 'REPLACE(' + @sql + ',''' + REPLACE(' '+Original_Word+' ','''','''''') + ''',''' + REPLACE(' '+Replace_Word+' ','''','''''') + ''')'
FROM Table2;

SET @sql = 'SELECT LTRIM(RTRIM(' + @sql + ')) FROM Table1';

PRINT(@sql)
EXECUTE (@sql);

这篇关于用另一个表中的词替换句子中的词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆