TSQL 字符串函數:截斷和查找


字符串截斷函數是指:Stuff 和 SubString,字符串查找函數是:CharIndex 和 PatIndex

一,SubString 截取子串

最常用的字符串函數,用於截取特定長度的子串。

SUBSTRING ( expression ,start , length )

參數說明:

  • start 參數:整數,表示開始位置;字符的序號(index)從1開始,即第一個字符的序號是1;
  • length參數:整數,表示截取字符的最大數量;如果start+Length 大於字符串的總長度,那么返回從Start開始的所有字符;
  • 返回data type:如果expression是char或varchar,那么返回varchar;如果expression是nchar或nvarchar,那么返回nvarchar;

例如:字符串 abcdef,截取從第二個字符開始,共2個字符的子串是:bc

select substring('abcdef',2,2)

二,Stuff 刪除字符串的指定部分,並插入相應的字符,即將字符串中指定部分替換為新的字符串

Stuff函數從字符串指定的開始位置,刪除指定長度的字符串,並從該位置插入新的字符串。

It deletes a specified length of characters in the first string at the start position and then inserts the second string into the first string at the start position.

STUFF ( character_expression , start , length , replaceWith_expression )

參數說明:

  • start:整數,表示刪除和插入的開始位置;如果start是0,或大於字符串的總長度,那么該函數返回NULL;
  • length:整數,表示刪除字符的最大數量;如果Length+Start大於字符串的總長度,表示刪除從Start開始的所有字符;
  • replaceWith_expression :字符類型,表示從開始位置(start)插入的字符串;

例如:從不同位置截斷字符串abcdef,查看返回結果

declare @str varchar(6)
set @str='abcdef'

select  stuff(@str,7,1,''),
        stuff(@str,0,1,''),
        stuff(@str,3,7,''),
        stuff(@str,3,7,'12345')

使用stuff函數,必須注意兩點:

  1. 如果Length+Start大於字符串的總長度,刪除從Start開始的所有字符;
  2. 如果Start是0,或大於字符串的總長度,返回Null;

三,CharIndex 從字符串中查找字符

從字符串search中查找另一個字符串find,如果find存在於search中,返回find在search中第一次匹配的開始位置;如果find不存在於search中,返回0;

CHARINDEX ( find ,search [ , start ] )

參數說明:

  • 在字符串search中查找字符串find,查找的開始位置由參數start確定;
  • 如果start參數沒有指定,默認從字符串search的第一個字符開始查找;
  • 如果find 或 search 是null,返回null;
  • 返回值是整數:如果從search中查找到find,那么返回第一次匹配的開始位置;如果查找不到,返回0;

例如:從字符串abcbcdef的不同位置,查找不同的字符

select charindex('bc','abcbcdef'),
        charindex('bc','abcbcdef',3),
        charindex('bce','abcbcdef')

結果分析:

  • bc 在 abcbcdef 中出現多次,從第1個字符開始查找,該函數只返回第一次匹配的開始位置;
  • bc 在 abcbcdef 中出現多次,從第3個字符開始查找,該函數只返回第一次匹配的開始位置;
  • bce 不存在 abcbcdef 中,該函數返回0;

四,PatIndex 從字符串中按照Pattern查找特定字符

PatIndex 從字符串中查找pattern,返回第一次匹配成功的開始位置;如果匹配失敗,返回0;

Returns the starting position of the first occurrence of a pattern in a specified expression, or zeros if the pattern is not found.

PATINDEX ( '%pattern%' , expression )

pattern:樣式字符,能夠使用通配符(%,_,[],^),必須以通配符%開始和結尾

示例:從字符串abcbcdef的匹配不同的pattern

select patindex('%bc%','abcbcdef'),
        patindex('%b_b%','abcbcdef'),
        patindex('%b[^c]b%','abcbcdef'),
        patindex('%bce%','abcbcdef')

結果分析:

  • Pattern: %bc%  表示bc字符前面和后面有0或多個字符
  • Pattern: %b_b%  表示b和b之間有一個字符,其前面和后面有0或多個字符
  • Pattern: %b[^c]b%  表示b和b之間有一個字符,該字符不能是c,其前面和后面有0或多個字符

五,在使用STUFF 函數時,注意,Start 參數不能是0 或大於字符串的總長度

使用轉換函數Convert(varchar(50), Datetime, 127),將日期/時間類型轉換成字符串類型,保留的毫秒位數是不固定的:當毫秒不為0時,顯式3位毫秒;當毫秒為0時,不顯示毫秒。Convert(varchar(50), Datetime, 127) 返回的字符串的格式是 yyyy-mm-ddThh:mi:ss.[mmm]  ,注意:如果毫秒是0,毫秒不顯式。

When the value for milliseconds (mmm) is 0, the milliseconds value is not displayed. For example, the value '2012-11-07T18:26:20.000 is displayed as '2012-11-07T18:26:20'.

樓主遇到一個問題,是127和stuff函數一起使用時產生的

1,創建示例數據

declare @dt1 datetime
declare @dt2 datetime

set @dt1='2015-01-02 01:23:45.678'
set @dt2='2015-01-02 01:23:45.000'

select CONVERT(VARCHAR(50),@dt1, 127),len(CONVERT(VARCHAR(50),@dt1, 127)),
        CONVERT(VARCHAR(50),@dt2, 127),len(CONVERT(VARCHAR(50),@dt2, 127))

2,如果 stuff( convert(varchar(50),@datetime,127),20,4) 會出現一個“意外”的結果

declare @dt1 datetime
declare @dt2 datetime

set @dt1='2015-01-02 01:23:45.678'
set @dt2='2015-01-02 01:23:45.000'

select CONVERT(VARCHAR(50),@dt1, 127),len(CONVERT(VARCHAR(50),@dt1, 127)),
        CONVERT(VARCHAR(50),@dt2, 127),len(CONVERT(VARCHAR(50),@dt2, 127)),
        STUFF(CONVERT(VARCHAR(50),min(@dt1), 127),20,4,'') ,
        STUFF(CONVERT(VARCHAR(50),min(@dt2), 127),20,4,'')

原因是:如果 start position 比字符串的長度大,stuff 返回NULL。

STUFF ( character_expression , start , length , replaceWith_expression )

在使用Stuff函數,必須注意:

  • If the starting position is larger than length of the first string, a null string is returned.
  • If the start position is 0, a null value is returned.

六,其他字符串函數

函數ASCII(character_expression) 和 UNICODE(character_expression) 用於獲取第一個字符的編碼,根據整數編碼,通過函數CHAR(code)和NCHAR(code),轉換為相應的字符。

函數REPLACE ( string_expression , string_pattern , string_replacement )  用於替換字符

在項目中,有時會遇到清空特定字符的情況,回車、換行、空格的ASCII碼值

  • 回車,ASCII碼13
  • 換行,ASCII碼10
  • 空格,ASCII碼32

例如,示例字符返回的ASCII是32,是空格的ASCII編碼,但是使用函數LTRIM和RTRIM,怎么都清不掉

select ascii(left(' http://apievangelist.com ',1))

你看到的空格,有可能是換行,其ASCII編碼是10,通過REPLACE函數替換“空格”:

replace(' http://apievangelist.com ',char(10),'')

 

參考文檔:

PATINDEX (Transact-SQL)
CHARINDEX (Transact-SQL)
SUBSTRING (Transact-SQL)
STUFF (Transact-SQL)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM