views:

165

answers:

6

Hi,

suppose I have a number 62. This is composed of 2 digits

How can add 2 digit together divide by 10 and if result = something like 6.2 just take reminder

 declare @Number int,@Result int
 set @Number =62

 if len(@Number) > 1
 set  @Result=????=--Add 6 and 2 =8

 set @result=@result % 10  --Mod operator

  print @result 

 -- the result should be 2 in this case

what Am I doing wrong?

Thanks a lot

+1  A: 

I think @Number % 10is what you are looking for. It returns the last digit of any number. 62 -> 2, 97 -> 7, etc...

Update:

I may have misunderstood the question. Maybe you want 10 % ((@Number / 10) + @Number) instead.

(@Number / 10) + @Number is the sum of the digits of a two-digit number.

Mark Byers
Although you could be right, and I misunderstand the question, I believe this just happens to be a rare occurrence in which `@Number % 10` is equal to each `10 % [added digits of @Number]`. Usually this is not the case.
Gausie
OK, thanks, I think you might be right. The question is very confusing.
Mark Byers
A: 

If the number is always two digits, then I would use LEFT(@Number,1) and RIGHT(@Number,1) to access each digit. If it's not, comment back and I'll give you some help with the loop you'll need.

More pressing an issue is that you're expecting 2, but the result of 8 mod 10 is 8. If you're looking for 2, the sum is 10 mod 8 (10 % @Result).

In any case, hit me back if this doesn't answer exactly what you wanted.

Gausie
+1  A: 
set @tens = floor(@Number / 10);
set @ones = @number - @tens;
set @Result = @tens + @ones;

Or use left and right to access substrings.

Donnie
+1  A: 

I have no way to try it:

DECLARE @Number VARCHAR(2) 
SET @Number = '62'

declare @firstNum INT, @secondNum INT
SET @firstNum = CAST(SUBSTRING(@Number, 1, 1) AS INT)
SET @secondNum = CAST(SUBSTRING(@Number, 2, 1) AS INT)

DECLARE @Result int

SET @Result = (@firstNum + @secondNum) % 10
Alex Bagnolini
that's what the title of the question seems to ask for, yes
marc_s
sorry if the question is confusingthanks a lot for your help.we are nearly thereGiven a number Work out if len(number) >1 if soadd the 2 numbers together (suppose now the total is 62)divide the result by 10= 6.2take reminder only mod operator "%" result should be 2How Can I adjust it?it's not homework!!! It's an algoritm in order to calculate a digit
jo
How can adding the two digits give '62' as a result? 6+2=8, not 62.
Mark Byers
A: 

Try this

declare @myNumber float,@result float  
set @myNumber = 62 

select 
@result = (case when len(@myNumber) = 1 then @myNumber 
  else (@myNumber /10) + cast(@myNumber as int) % 10 end)

select [Output] = STUFF(
    cast(@result as varchar(50))
    ,1
    ,charindex('.',cast(@result as varchar(50)))    
    ,'')

Output
2

Hope this helps

And if u just want to add 2 numbers try this(though it has been implemented above)

declare @myNumber int,@result int  
set @myNumber = 62 

select 
Result = (case when len(@myNumber) = 1 then @myNumber 
else (@myNumber /10) + @myNumber % 10 end)

Result
8
priyanka.sarkar
A: 

So I guess you want something like this - parse the string representing your number, adding up the individual digits as integer values. This gives you a total result at the end - then you do whatever you need to do with that. This code works for any length of string (up to 50 characters = 50 digits in your original number):

DECLARE @Number INT 
SET @Number = 62

DECLARE @NumString VARCHAR(50)
SET @NumString = CAST(@Number AS VARCHAR(50))

DECLARE @Index INT
SET @Index = 1

DECLARE @Sum INT
SET @Sum = 0

WHILE @Index <= LEN(@NumString)
BEGIN
   SET @Sum = @Sum + CAST(SUBSTRING(@NumString, @Index, 1) AS INT)
   SET @Index = @Index + 1
END

SELECT @Sum AS 'Sum of all digits'

With the initial value of "62" for @Number, I get a result of 8 - now you can continue on using that value.

If you need this function often, I would probably encapsulate it into a user-defined function so you can call it from everywhere in your code.

marc_s