Replace words in a sentence with words from another table(用另一个表中的词替换句子中的词)
本文介绍了用另一个表中的词替换句子中的词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 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);
这篇关于用另一个表中的词替换句子中的词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:用另一个表中的词替换句子中的词
基础教程推荐
猜你喜欢
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01