About Me

My photo
જય વાળીનાથ
T-SQL is a basic of structure query language. So I always want to learn T-SQL in different way to get best performance in query. And its my passion.

Monday, April 7, 2008

Sum Of Digits Script in T-SQL

Here this script display sum of digits of @acceptedNo = 123.


declare @acceptedNo int
declare @result int
declare @rem int
set @acceptedNo = 123
set @result = 0
print 'Input no :' + cast(@acceptedNo as varchar)
while(@acceptedNo != 0)
begin
set @rem = @acceptedNo % 10
set @result = (@result + @rem )
set @acceptedNo = @acceptedNo / 10
end
print 'Sum of digits of Input no : ' + cast(@result as varchar)

Output will be...

Input no :123
Sum of digits of Input no : 6

1 comment:

BHAVIN SONI said...

This Program is Only Run for 123 , not for other value.so, check it.