(
@InputString NVARCHAR(MAX)
,@RightValues INT
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE @Count INT = 0
,@Len INT
,@Output NVARCHAR(MAX) =''
SET @Len = LEN(@InputString)
IF @Len < @RightValues
BEGIN
RETURN NULL
END
ELSE
BEGIN
WHILE (@Count < @RightValues)
BEGIN
SET @Output = SUBSTRING(@InputString, @Len-@Count,1) + @Output
SET @Count = @Count + 1
END
END
RETURN @Output
END
SELECT dbo.RightValues('abc',4)