GOCREATE FUNCTION ExtStuff(@str NVARCHAR(MAX),@start INT,@len INT,@rep NVARCHAR(MAX))RETURNS NVARCHAR(MAX)ASBEGIN DECLARE @rts NVARCHAR(MAX); SELECT @rts=CASE WHEN @start>0 THEN STUFF(@str,@start,@len,@rep) WHEN @start<0 THEN REVERSE(STUFF(REVERSE(@str),-@start,@len,REVERSE(@rep))) ELSE NULL END; RETURN @rts;END
例:
SELECT dbo.ExtStuff('123',3,1,'97')//OUTPUT:1297SELECT dbo.ExtStuff('123',-1,1,'97')
//OUTPUT:1297