CREATE FUNCTION [dbo].[substr2_nvarchar](@src as nvarchar(max), @pos as int)
returns nvarchar(max)
begin
declare
@strlen as int
if( @src is NULL) or (@pos is NULL )
return NULL;
select @strlen=[dbo].[length_nvarchar](@src)
if abs(@pos) > @strlen
return NULL;
if @pos = 0
select @pos = 1
if @pos > 0
begin
return substring(@src, @pos, @strlen-@pos+1)
end
return substring(@src, @strlen + @pos+1, -@pos)
end
GO