CREATE FUNCTION [dbo].[SplitString]
(
@Expression nvarchar(4000),--要拆分的字符串
@splitchar nvarchar (100),--拆分字符
@n int --要獲得已拆分的返回字符串的位置
)
RETURNS nvarchar(4000)
AS
BEGIN
-- Declare the return variable here
DECLARE @p int
set @p=CHARINDEX(@splitchar,@Expression)
if @p>0
begin
set @p=@p+LEN(@splitchar)-1
end
declare @i int
set @i=1
while @i<@n
begin
set @i=@i+1
set @Expression=SUBSTRING(@Expression,@p+1,LEN(@Expression)-@p)
set @p=CHARINDEX(@splitchar,@Expression)
if @p>0
begin
set @p=@p+LEN(@splitchar)-1
end
else
begin
break
end
end
declare @s Nvarchar(1000)
if @p=0 AND @i=@n
begin
set @s=@Expression
end
else
if @i=@n
begin
set @s=SUBSTRING(@Expression,1,@p-LEN(@splitchar))
end return @s
end
-- Add the T-SQL statements to compute the return value here
GO
函數的引用:express