SqlServer 帶分隔符字串轉表格語句
阿新 • • 發佈:2018-12-20
--單語句
with t as ( select '1,2,3,4,5,6,7,8,9999' c,0 l union all select right(c,len(c)-CHARINDEX(',',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,0 l union all select right(c,len(c)-CHARINDEX(@div,c)),CHARINDEX(@div,c) from t where CHARINDEX(@div,c)>0 ), t1 as ( select [email protected] c from t ) select left(c,charindex(@div,c)-1) from t1 OPTION (MAXRECURSION 0);
end