SqlServer 帶分隔符字串轉表格語句

--單語句

with t as (
select '1,2,3,4,5,6,7,8,9999' c
union all
select right(c,len(c)-CHARINDEX(',',c)) from t where CHARINDEX(',',c)>0
),
t1 as (
select c+',' c from t
)
select left(c,charindex(',',c)-1) from t1 OPTION (MAXRECURSION 0)

 


--塊代碼

begin

declare @src varchar(1024) = '1,2,3,4,5,6,7,8,9999';
declare @div varchar(1) = ',';

with t as (
select @src c
union all
select right(c,len(c)-CHARINDEX(@div,c)) from t where CHARINDEX(@div,c)>0
),
t1 as (
select c+@div c from t
)
select left(c,charindex(@div,c)-1) from t1 OPTION (MAXRECURSION 0);

end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章