sqlserver以逗號分割的字串拆分到臨時表
阿新 • • 發佈:2018-12-30
alter FUNCTION [dbo].[func_split](@str nvarchar(4000),@separtor varchar(10))
returns @temp table([row] [int] IDENTITY(1,1) NOT NULL,valuess nvarchar(4000))
as
begin
declare @i int
set @str=rtrim(ltrim(@str))
set @i=charindex(@separtor,@str)
while @i>=1
begin
insert @temp values(left(@str,@i-1))
set @str=substring(@str,@i+1,len(@str) [email protected])
set @i=charindex(@separtor,@str)
end
if @str<>''
insert @temp values(@str)
return
returns @temp table([row] [int] IDENTITY(1,1) NOT NULL,valuess nvarchar(4000))
as
begin
declare @i int
set @str=rtrim(ltrim(@str))
set @i=charindex(@separtor,@str)
while @i>=1
begin
insert @temp values(left(@str,@i-1))
set @str=substring(@str,@i+1,len(@str)
set @i=charindex(@separtor,@str)
end
if @str<>''
insert @temp values(@str)
return
end
select * from dbo.func_split('1,2,3,4,5,7', ',')