alter function FormatStr(@str varchar(1000))
returns varchar(1000)
as
begin
--declare @str varchar(50)
declare @lenStr int,@i int,@index int
declare @tmpStr varchar(1000)
declare @lastChar bit
set @lastChar = 0
set @i = 1
--set @str = 'R11-103422,R11-103455,R11-103421,R11-103420'
set @lenStr = len(@str);
set @tmpStr =''
set @index = 0;
while (1=1)
begin
if CharIndex(',',@str,0) = 0
begin
Set @LastChar = 1;
set @tmpStr = @tmpStr + '''' + @str + '''';
set @tmpStr = '(' + @tmpStr + ')'
return @tmpStr
end
set @index = CharIndex(',',@str,0);
--set @tmpStr = SubString(@str,1,@index);
set @lenStr =len(@str);
if @LastChar = 0
begin
set @tmpStr = @tmpStr + '''' + SubString(@str,1,@index-1) + ''','
set @str = SubString(@str,@index+1,@lenStr-@index)
--set @i = @i +1
end
--else
--begin
--set @tmpStr = @tmpStr + '''' + SubString(@str,1,@index-1) + ''''
--set @str = SubString(@str,@index+1,@lenStr-@index)
--set @i = @i+1
--break;
--end
end
set @tmpStr = '(' + @tmpStr + ')'
return @tmpStr
end
go