參數名 | 解釋 |
---|---|
str | 需要拆分的字符串 |
delim | 分隔符,通過某字符進行拆分 |
count | 當 count 為正數,取第 n 個分隔符之前的所有字符; 當 count 為負數,取倒數第 n 個分隔符之后的所有字符。 |
2、 舉例
(1)獲取第2個以“,”逗號為分隔符之前的所有字符。
SUBSTRING_INDEX('7654,7698,7782,7788',',',2)
(2)獲取倒數第2個以“,”逗號分隔符之后的所有字符
SUBSTRING_INDEX('7654,7698,7782,7788',',',-2)
1、參數解說
參數名 | 解釋 |
---|---|
str | 需要進行替換的字符串 |
from_str | 需要被替換的字符串 |
to_str | 需要替換的字符串 |
2、 舉例
(1)將分隔符“,”逗號替換為“”空。
REPLACE('7654,7698,7782,7788',',','')
1、參數解說
參數名 | 解釋 |
---|---|
str | 需要計算長度的字符串 |
2、舉例
(1)獲取 ‘7654,7698,7782,7788' 字符串的長度
LENGTH('7654,7698,7782,7788')
實現的SQL解析
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('7654,7698,7782,7788',',',help_topic_id+1),',',-1) AS num FROM mysql.help_topic WHERE help_topic_id LENGTH('7654,7698,7782,7788')-LENGTH(REPLACE('7654,7698,7782,7788',',',''))+1
此處利用 mysql 庫的 help_topic 表的 help_topic_id 來作為變量,因為 help_topic_id 是自增的,當然也可以用其他表的自增字段輔助。
help_topic 表:
實現步驟:
Step1:首先獲取最后需被拆分成多少個字符串,利用 help_topic_id 來模擬遍歷 第n個字符串。
涉及的代碼片段:
help_topic_id LENGTH('7654,7698,7782,7788')-LENGTH(REPLACE('7654,7698,7782,7788',',',''))+1
Step2:根據“,”逗號來拆分字符串,此處利用 SUBSTRING_INDEX(str, delim, count) 函數,最后把結果賦值給 num 字段。
涉及的代碼片段:
SUBSTRING_INDEX(SUBSTRING_INDEX('7654,7698,7782,7788',',',help_topic_id+1),',',-1) AS num
第一步:
以”,”逗號為分隔符,根據 help_topic_id 的值來截取第n+1個分隔符之前所有的字符串。 (此處 n+1 是因為help_topic_id 是從0開始算起,而此處需從第1個分隔符開始獲取。)
SUBSTRING_INDEX('7654,7698,7782,7788',',',help_topic_id+1)
eg:
當 help_topic_id = 0時,獲取到的字符串 = 7654
當 help_topic_id = 1時,獲取到的字符串 = 7654,7698
…(以此類推)
第二步:
以”,”逗號為分隔符,截取倒數第1個分隔符之后的所有字符串。
SUBSTRING_INDEX(SUBSTRING_INDEX('7654,7698,7782,7788',',',help_topic_id+1),',',-1)
eg:
根據第一步,當 help_topic_id = 0時,獲取到的字符串 = 7654,此時第二步截取的字符串 = 7654
根據第一步,當 help_topic_id = 1時,獲取到的字符串 = 7654,7698,此時第二步截取的字符串 = 7698
…(以此類推)
最終成功實現了以下效果 ~
注:不含分隔符的字符串拆分可參考 MySQL——字符串拆分(無分隔符的字符串截取)
補充:mysql字段分隔符拆分_MySQL里實現類似SPLIT的分割字符串的函數
下邊的函數,實現了象數組一樣去處理字符串。
create function f_split(@c varchar(2000),@split varchar(2)) returns @t table(col varchar(20)) as begin while(charindex(@split,@c)>0) begin insert @t(col) values (substring(@c,1,charindex(@split,@c)-1)) set @c = stuff(@c,@c),'') end insert @t(col) values (@c) return end go select * from dbo.f_split('dfkd,dfdkdf,dfdkf,dffjk',',') drop function f_split col -------------------- dfkd dfdkdf dfdkf dffjk
(所影響的行數為 4 行)
返回分割后的元素個數,方法很簡單,就是看字符串中存在多少個分隔符號,然后再加一,就是要求的結果。
CREATE function Get_StrArrayLength ( @str varchar(1024),--要分割的字符串 @split varchar(10) --分隔符號 ) returns int as begin declare @location int declare @start int declare @length int set @str=ltrim(rtrim(@str)) set @location=charindex(@split,@str) set @length=1 while @location>0 begin set @start=@location+1 set @location=charindex(@split,@str,@start) set @length=@length+1 end return @length end
調用示例:
select dbo.Get_StrArrayLength('78,2,3',')
返回值:4
返回分割后指定索引的第幾個元素,象數組一樣方便
CREATE function Get_StrArrayStrOfIndex ( @str varchar(1024),--要分割的字符串 @split varchar(10),--分隔符號 @index int --取第幾個元素 ) returns varchar(1024) as begin declare @location int declare @start int declare @next int declare @seed int set @str=ltrim(rtrim(@str)) set @start=1 set @next=1 set @seed=len(@split) set @location=charindex(@split,@str) while @location>0 and @index>@next begin set @start=@location+@seed set @location=charindex(@split,@start) set @next=@next+1 end if @location =0 select @location =len(@str)+1 --這兒存在兩種情況:1、字符串不存在分隔符號 2、字符串中存在分隔符號,跳出while循環后,@location為0,那默認為字符串后邊有一個分隔符號。 return substring(@str,@start,@location-@start) end
調用示例:
select dbo.Get_StrArrayStrOfIndex('8,9,4',2)
返回值:9
declare @str varchar(50) set @str='1,3,4,5' declare @next int set @next=1 while @next=dbo.Get_StrArrayLength(@str,') begin print dbo.Get_StrArrayStrOfIndex(@str,@next) set @next=@next+1 end
調用結果:
1
2
3
4
5
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。