简体   繁体   English

在sql中替换部分字符串

[英]Replace parts of a string in sql

I have a column which contains the string in the below format:我有一列包含以下格式的字符串:

Account Number [1.######] belongs to customer [2.########] residing in [3.######] for the selected period. 

I want to replace the placeholders of 1, 2 and 3 with actual value which will come from a table.我想用来自表格的实际值替换 1、2 和 3 的占位符。

Something like below:如下所示:

Account Number 1234 belongs to John residing in London for the selected period. 

Any help would be greatly appreciated.任何帮助将不胜感激。

Thanks in advance.提前致谢。

Regards, Ram问候,拉姆

Starting from SQL Server 2016 onwards, it supports an enhanced version of the FORMATMESSAGE() function.从 SQL Server 2016 开始,它支持FORMATMESSAGE()函数的增强版本。

Message can have a maximum of 2,047 characters.消息最多可包含 2,047 个字符。

Here is a conceptual example of it.这是一个概念性的例子。

SQL SQL

DECLARE @message VARCHAR(2048) = 'Account Number %s belongs to customer %s residing in %s for the selected period.';
DECLARE @AccountNo VARCHAR(20) = '1234'
    , @Customer VARCHAR(20) = 'John'
    , @Location VARCHAR(20) = 'London'
    , @Result VARCHAR(2048);

SET @Result = FORMATMESSAGE(@message
            , @AccountNo
            , @Customer
            , @Location);

-- test
SELECT @Result;

Output输出

Account Number 1234 belongs to customer John residing in London for the selected period.

在您的选择语句中添加以下内容:

[New Field] = CONCAT('Account ',table.field,' belongs to customer ',table.field,'residing in 'table.field' for theselected period.')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM