CREATE FUNCTION ADD_DIGITS ( @number int ) RETURNS int AS /************************************************************************************************* Function: ADD_DIGITS (Copyright © 2001 Narayana Vyas Kondreddi. All rights reserved.) Purpose: To add all the individual digits within a give number Written by: Narayana Vyas Kondreddi http://vyaskn.tripod.com Tested on: SQL Server 7.0 (as a procedure. UDFs are not supported in 7.0) and SQL Server 2000 Date modified: March-22-2001 5:30 PM Email: vyaskn@hotmail.com *************************************************************************************************/ BEGIN DECLARE @ctr int SET @ctr = 0 WHILE @number >= 10 BEGIN SET @ctr = @ctr + (@number % 10) SET @number = @number / 10 END RETURN @ctr + @number END