簡體   English   中英

在sql中替換部分字符串

[英]Replace parts of a string in sql

我有一列包含以下格式的字符串:

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

我想用來自表格的實際值替換 1、2 和 3 的占位符。

如下所示:

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

任何幫助將不勝感激。

提前致謝。

問候,拉姆

從 SQL Server 2016 開始,它支持FORMATMESSAGE()函數的增強版本。

消息最多可包含 2,047 個字符。

這是一個概念性的例子。

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;

輸出

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