sql 字串按指定字元轉多行(錶值函式)
方法一
select * from splittotable('12+3+4545+2322','+')
select * from splittotable('12,3,4545,2322',',')
alter function [dbo].[splittotable]
(@splitstring nvarchar(max), --拆解字串
@separator nvarchar(10)=' ' --拆解標識(根據什麼字元拆解)
)returns @splitstringstable table --返回表結構
([value] nvarchar(max))as
begin
insert into @splitstringstable
select b.strstring
from(
select strstring=convert(xml,''+replace(rtrim(ltrim(@splitstring)),@separator,'
')+'
')) a
select strstring = c.v.value('.','nvarchar(max)')
from a.strstring.nodes('/root/v') c(v)
) breturn;
end方法二
select * from splittotable('12+3+4545+2322','+')
select * from splittotable('12,3,4545,2322',',')
alter function [dbo].[splittotable]
(@splitstring nvarchar(max), --拆解字串
@separator nvarchar(10)=' ' --拆解標識(根據什麼字元拆解)
)returns @splitstringstable table --返回表結構
([id] int identity(1,1),
[value] nvarchar(max))as
begin
declare @currentindex int;
declare @nextindex int;
declare @returntext nvarchar(max);
select @currentindex=1;
while(@currentindex<=len(@splitstring))
begin
select @nextindex=charindex(@separator,@splitstring,@currentindex);
if(@nextindex=0 or @nextindex is null)
select @nextindex=len(@splitstring)+1;
select @returntext=substring(@splitstring,@currentindex,@nextindex-@currentindex);
insert into @splitstringstable([value]) values(@returntext);
select @currentindex=@nextindex+1;
endreturn;
end
mysql 按指定字元拆分字串
sql語句 set str 7654,7698,7782,7788 select substring index substring index str,help topic id 1 1 as numfrom mysql.help topic where help topic id length ...
SQL按指定符號分割字串函式
一 sql分割字串,返回臨時表 方法一 1 create function dbo f split 2 3 cvarchar 2000 需要分割的字串 例如 1,2,3,4,5 我 和 你 4 split varchar 2 分隔符 例如 5 6returns ttable col varchar ...
SQL字串分割多行
原型資料 需求 按逗號或分號將字段it description值分割為多行,填充下拉列表 sql實現指令碼 select b.vv from select convert xml,replace replace it description,as xmlval from tb invoicetype...