SQL 指定字串分隔位置傳出字串
/*******************************************************************
功能:指定字串分隔位置傳出字串
說明:
參數:@c:字串
@split:分隔符號
@loc:以@split分隔之第N個字串
Create Date :2011/05/12
執行範例:select dbo.ufnSplitLoc('a,b,c',',',3)
********************************************************************/
ALTER FUNCTION [dbo].[ufnSplitLoc]
(
@c varchar(3000),@split varchar(2),@loc int
)
RETURNS varchar(50)
AS
BEGIN
-- Declare the return variable here
declare @count int
set @count=1
while(charindex(@split,@c)<>0)
begin
if(@count<@loc)
begin
set @c = stuff(@c,1,charindex(@split,@c),'')
set @count=@count+1
end
else
break;
end
if(charindex(@split,@c)>0)
set @c=substring(@c,1,charindex(@split,@c)-1)
if @count < @loc
set @c=''
return @c
END