SQL SERVER 正則替換

來源:互聯網
上載者:User

標籤:replace   with   function   out   int   cti   函數   returns   method   

use pubdb
go

IF OBJECT_ID(N‘dbo.RegexReplace‘) IS NOT NULL
 DROP FUNCTION dbo.RegexReplace
GO
--開始建立正則替換函數
 CREATE FUNCTION DBO.RegexReplace
(
 @string VARCHAR(MAX), --被替換的字串
 @pattern VARCHAR(255), --替換模板
 @replacestr VARCHAR(max), --替換後的字串
 @IgnoreCase INT = 0 --0區分大小寫 1不區分大小寫
)
RETURNS VARCHAR(max)
AS
BEGIN
 DECLARE @objRegex INT, @retstr VARCHAR(8000)
 --建立對象
 EXEC sp_OACreate ‘VBScript.RegExp‘, @objRegex OUT
 --設定屬性
 EXEC sp_OASetProperty @objRegex, ‘Pattern‘, @pattern
 EXEC sp_OASetProperty @objRegex, ‘IgnoreCase‘, @IgnoreCase
 EXEC sp_OASetProperty @objRegex, ‘Global‘, 1
 --執行
 EXEC sp_OAMethod @objRegex, ‘Replace‘, @retstr OUT, @string, @replacestr
 --釋放
 EXECUTE sp_OADestroy @objRegex
 RETURN @retstr
END
GO

--保證正常啟動並執行話,需要將Ole Automation Procedures選項置為1
EXEC sp_configure ‘show advanced options‘, 1
RECONFIGURE WITH OVERRIDE
EXEC sp_configure ‘Ole Automation Procedures‘, 1
RECONFIGURE WITH OVERRIDE



SQL SERVER 正則替換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.