sql server 字符串 in 轉化成 列表


效果如圖

 

 

 

 

 


--該函數把傳遞過來的字符串轉換成IN 后面的列表,可以處理以分號,逗號以及空格分隔的字符串
CREATE FUNCTION [dbo].[GetInStr]
(@SourceStr varchar(8000))--源字符串

RETURNS @table table(list varchar(100) )
AS
BEGIN

select @sourcestr = replace(@sourcestr,';',',')
select @sourcestr = replace(@sourcestr,'\',',')
--select @sourcestr = replace(@sourcestr,'.',',')
-- select @sourcestr = replace(@sourcestr,' ',',')
--declare @OutStr varchar(200)

if charindex(',',@sourcestr)>0
begin
declare @i int
declare @n int
set @i=1
while charindex(',',@sourcestr,@i)>0
begin
set @n=charindex(',',@sourcestr,@i)
insert into @table values(substring(@sourcestr,@i, @n-@i) )
set @i=@n+1
end
insert into @table values(substring(@sourcestr,@i,len(@sourcestr)-@i+1))
end else insert into @table values(@sourcestr)

delete from @table where isnull(list,'') = ''
return
END



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM