久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

SQL Server 2014 替換為正則表達式

SQL Server 2014 replace with regex(SQL Server 2014 替換為正則表達式)
本文介紹了SQL Server 2014 替換為正則表達式的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

替換列中字符串的最佳方法是什么:

假設我有一個列類型為 varchar 的表,可能的值包含:

'sample text min(my value) 用 ) 和 ('

注意:我的價值可能會有所不同.

現在我想將其替換為:

min(max(my value))

所以在這種情況下的最終值是:

'sample text min(max(my value)) 用 ) 和 ('

我想對整個表執行更新.

是否可以使用純 T-SQL?

所以 => 之前和轉換之后的兩個示例行:

<代碼>1.值為:min(10).=>值為: min(max(10))2. 樣本 min(cat) =>樣本最小值(最大值(貓))

基本上用min(max(value))替換所有出現的min(value),其中'value'可以是任何字符串

解決方案

首先你需要這個用戶定義的函數來搜索用字符串替換模式:

CREATE FUNCTION dbo.PatternReplace(@InputString VARCHAR(4000),@Pattern VARCHAR(100),@ReplaceText VARCHAR(4000))返回 VARCHAR(4000)作為開始聲明 @Result VARCHAR(4000) SET @Result = ''-- 匹配中的第一個字符聲明@First INT-- 下一個開始搜索的字符聲明 @Next INT SET @Next = 1-- 總字符串的長度 -- 如果@InputString 為 NULL,則為 8001聲明@Len INT SET @Len = COALESCE(LEN(@InputString), 8001)-- 模式結束聲明 @EndPattern INT而 (@Next <= @Len)開始SET @First = PATINDEX('%' + @Pattern + '%', SUBSTRING(@InputString, @Next, @Len))IF COALESCE(@First, 0) = 0 -- 不匹配 - 返回開始SET @Result = @Result +CASE --return NULL, 就像 REPLACE, 如果輸入是 NULL當@InputString 為空時或 @Pattern 為空或 @ReplaceText 為 NULL THEN NULLELSE SUBSTRING(@InputString, @Next, @Len)結尾休息結尾別的開始-- 將匹配之前的字符連接到結果SET @Result = @Result + SUBSTRING(@InputString, @Next, @First - 1)SET @Next = @Next + @First - 1設置@EndPattern = 1-- 查找結束模式范圍的開始而 PATINDEX(@Pattern, SUBSTRING(@InputString, @Next, @EndPattern)) = 0設置@EndPattern = @EndPattern + 1-- 查找模式范圍的結尾而 PATINDEX(@Pattern, SUBSTRING(@InputString, @Next, @EndPattern)) >0AND @Len >= (@Next + @EndPattern - 1)SET @EndPattern = @EndPattern + 1-- 在模式末尾或@Next + @EndPattern = @LenSET @Result = @Result + @ReplaceTextSET @Next = @Next + @EndPattern - 1結尾結尾返回(@結果)結尾

閱讀更多

min(xxx) 出現不止一次:

最后,您可以簡單地更新您的表格,如下所示:

更新你的表SET YourColumn = REPLACE(dbo.PatternReplace(YourColumn, '%min(', 'min(max('),'min(max(' + SUBSTRING(SUBSTRING(YourColumn,CHARINDEX('min(', YourColumn)+ 4,LEN(YourColumn)- CHARINDEX('min(',YourColumn)), 1,CHARINDEX(')', YourColumn)- ( CHARINDEX('min(', YourColumn) + 4 ))+ ')','min(max(' + SUBSTRING(SUBSTRING(YourColumn,CHARINDEX('min(', YourColumn)+ 4,LEN(YourColumn)- CHARINDEX('min(',YourColumn)), 1,CHARINDEX(')', YourColumn)- ( CHARINDEX('min(', YourColumn) + 4 ))+ '))');

What is the best way to replace string in column like:

Let's say I have table with column type varchar and with the possible value that contains:

'sample text min(my value) continue with sample text with ) and ('

Note: my value can vary.

Now I would like to replace it with:

min(max(my value))

so that the final value would in that case be:

'sample text min(max(my value)) continue with sample text with ) and ('

I would like to perform update on the whole table.

Is it possible using pure T-SQL?

So two sample rows before => and after transformation:

1. the value is: min(10). => the value is: min(max(10))
2. sample min(cat) => sample min(max(cat)) 

Basically replace all occurrences of min(value) with min(max(value)) where 'value' can be any string

解決方案

First of all you need this user defined function to search for replacing a pattern with string:

CREATE FUNCTION dbo.PatternReplace
(
   @InputString VARCHAR(4000),
   @Pattern VARCHAR(100),
   @ReplaceText VARCHAR(4000)
)
RETURNS VARCHAR(4000)
AS
BEGIN
   DECLARE @Result VARCHAR(4000) SET @Result = ''
   -- First character in a match
   DECLARE @First INT
    -- Next character to start search on
    DECLARE @Next INT SET @Next = 1
    -- Length of the total string -- 8001 if @InputString is NULL
    DECLARE @Len INT SET @Len = COALESCE(LEN(@InputString), 8001)
    -- End of a pattern
    DECLARE @EndPattern INT

     WHILE (@Next <= @Len) 
     BEGIN
     SET @First = PATINDEX('%' + @Pattern + '%', SUBSTRING(@InputString, @Next, @Len))
      IF COALESCE(@First, 0) = 0 --no match - return
       BEGIN
          SET @Result = @Result + 
             CASE --return NULL, just like REPLACE, if inputs are NULL
                WHEN  @InputString IS NULL
                 OR @Pattern IS NULL
                 OR @ReplaceText IS NULL THEN NULL
           ELSE SUBSTRING(@InputString, @Next, @Len)
        END
     BREAK
  END
  ELSE
  BEGIN
     -- Concatenate characters before the match to the result
     SET @Result = @Result + SUBSTRING(@InputString, @Next, @First - 1)
     SET @Next = @Next + @First - 1

     SET @EndPattern = 1
     -- Find start of end pattern range
     WHILE PATINDEX(@Pattern, SUBSTRING(@InputString, @Next, @EndPattern)) = 0
        SET @EndPattern = @EndPattern + 1
     -- Find end of pattern range
     WHILE PATINDEX(@Pattern, SUBSTRING(@InputString, @Next, @EndPattern)) > 0
           AND @Len >= (@Next + @EndPattern - 1)
        SET @EndPattern = @EndPattern + 1

     --Either at the end of the pattern or @Next + @EndPattern = @Len
     SET @Result = @Result + @ReplaceText
     SET @Next = @Next + @EndPattern - 1
  END
      END
      RETURN(@Result)
   END

Read more here

After creating this function you can try this:

DECLARE @x VARCHAR(max)
SET @x = 'sample text min(my value) continue with sample text with ) and ('

DECLARE @val VARCHAR(max)

SET @val =  SUBSTRING(SUBSTRING(@x,CHARINDEX('min(',@x)+4,LEN(@x)-CHARINDEX('min(',@x)),1,CHARINDEX(')',@x)-(CHARINDEX('min(',@x)+4))

SELECT REPLACE(dbo.PatternReplace(@x,'%min(','min(max('),'min(max('+@val+')','min(max('+@val+'))')

And you can see that the output is:

Occurrence of min(xxx) more than once:

Finally you can simply update your table as below:

UPDATE  YourTable
SET     YourColumn = REPLACE(dbo.PatternReplace(YourColumn, '%min(', 'min(max('),
                   'min(max(' + SUBSTRING(SUBSTRING(YourColumn,CHARINDEX('min(', YourColumn)+ 4,LEN(YourColumn)- CHARINDEX('min(',YourColumn)), 1,CHARINDEX(')', YourColumn)- ( CHARINDEX('min(', YourColumn) + 4 ))+ ')',
                   'min(max(' + SUBSTRING(SUBSTRING(YourColumn,CHARINDEX('min(', YourColumn)+ 4,LEN(YourColumn)- CHARINDEX('min(',YourColumn)), 1,CHARINDEX(')', YourColumn)- ( CHARINDEX('min(', YourColumn) + 4 ))+ '))');

這篇關于SQL Server 2014 替換為正則表達式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 3p在线观看 | 成人a视频| 日韩天堂av | 天天综合网站 | 久久夜色精品国产欧美乱极品 | 久久视频在线 | 国产精品伦子伦免费视频 | 日韩欧美一区在线 | 日韩精品久久久久久 | 青青草在线免费视频 | 国产精品永久久久久久久久久 | 狠狠干影院 | 成人网在线 | 国产一区二区三区久久 | 亚洲一级片| 久久91视频| 91成人在线观看喷潮蘑菇 | 成人欧美一区二区三区黑人免费 | 欧美xxxx性| 国产精品亚洲精品 | 亚洲免费婷婷 | 97视频在线观看免费 | 久久久久久97 | 欧美日韩一区二 | 99国产精品99久久久久久粉嫩 | 精品一区二区三区三区 | av片在线看 | 日韩国产精品一区二区 | 黄色一级视频网站 | 国产成人精品一区二区三区在线观看 | 一级特黄视频 | 成人三级在线观看 | 亚洲日本中文字幕 | 欧美性色网 | 中文字幕色哟哟 | 精品视频一区二区三区 | 亚洲精品91 | 欧美日韩高清在线 | 欧美日在线 | 欧美高清视频在线观看mv | 午夜视频网 |