Try this version using a Tally table. No loops, no cursors.
IF OBJECT_ID ( 'tempdb..#Tally' ) IS NOT NULL
BEGIN
DROP TABLE #Tally
END
GO
DECLARE @AlphaNumber varchar ( 20 )
DEcLARE @numtoadd decimal ( 20, 4 )
DECLARE @NumPart decimal ( 20, 4 )
DECLARE @Result varchar ( 20 )
DECLARE @strlen int
DECLARE @numpos int
SET @AlphaNumber = 'ABC12345.6789'
SET @numtoadd = 0.3211
SET @strlen = DATALENGTH ( @AlphaNumber )
SELECT TOP ( @strlen )
IDENTITY ( int, 1, 1 ) AS N
INTO #Tally
FROM master.dbo.syscolumns
ALTER TABLE #Tally
ADD C char ( 1 )
UPDATE #Tally SET C = SUBSTRING ( @AlphaNumber, N, 1 )
--SELECT * FROM #Tally
SELECT @numpos = MIN ( N ) FROM #Tally WHERE C BETWEEN '0' AND '9' OR C = '.'
--PRINT @numpos
SET @NumPart = CAST ( SUBSTRING ( @AlphaNumber, @numpos, @strlen ) AS decimal ( 20, 4 ) )
--PRINT @NumPart
SET @NumPart = @NumPart + @numtoadd
SET @Result = LEFT ( @AlphaNumber, @numpos - 1 ) + LTRIM ( STR ( @NumPart, @strlen, 4 ) )
PRINT @AlphaNumber + ' + ' + LTRIM ( STR ( @numtoadd, 10, 4 ) ) + ' = ' + @Result
DROP TABLE #Tally
I know, it breaks if there is no numeric part or there is more than one decimal point 🙂
Regards,
Jan