Hi @RajKumar
please check this function :
create function [dbo].[TESTFUNC](@string VARCHAR(MAX))-- parameter
returns int --return type
AS
BEGIN
DECLARE @LENGTH INT = (SELECT LEN(@string))
declare @returnvalue int
IF @LENGTH <3
BEGIN
DECLARE @stringint INT = CONVERT(INT,@string)
SET @returnvalue = (SELECT IIF(@stringint>=10,@stringint/10+@stringint%10,@stringint))
END
ELSE
BEGIN
DECLARE @I INT = 1
DECLARE @STR VARCHAR(10)
DECLARE @SUM INT = 0
WHILE @I <=@LENGTH
BEGIN
SET @STR = SUBSTRING(@string,@I,1)
DECLARE @STRVALUE INT= CONVERT(INT,@STR)
DECLARE @IFEVEN int = IIF(@I%2=0,1,0)
SET @STRVALUE = IIF(@IFEVEN =0,@STRVALUE*2,@STRVALUE *1)
SET @STRVALUE = IIF(@STRVALUE>=10,@STRVALUE/10+@STRVALUE%10,@STRVALUE)
SET @I = @I+1
SET @SUM = @SUM+@STRVALUE
END
declare @strtemp VARCHAR(MAX)= cast(@SUM AS VARCHAR(MAX))
SET @returnvalue = dbo.TESTFUNC(@strtemp)
END
RETURN @returnvalue
END
and use it
SELECT dbo.TESTFUNC('0012345')
result:
Best Regards,
Isabella
If the answer is the right solution, please click "Accept Answer" and upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.