如何在 SQL Server 2000 中将 2 位数字相加

发布于 2024-08-12 18:47:46 字数 343 浏览 4 评论 0原文

假设我有一个数字 62。它由 2 位数字组成。

如何将 2 位数字相加除以 10,如果结果 = 类似 6.2 的值,请提醒

 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

我我做错了什么?

多谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

り繁华旳梦境 2024-08-19 18:47:46
set @tens = floor(@Number / 10);
set @ones = @number - @tens;
set @Result = @tens + @ones;

或者使用 leftright 访问子字符串。

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

Or use left and right to access substrings.

青丝拂面 2024-08-19 18:47:46

我没有办法尝试:

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

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
a√萤火虫的光℡ 2024-08-19 18:47:46

我认为 @Number % 10 就是您正在寻找的。它返回任何数字的最后一位数字。 62-> 2、 97 --> 7等...

更新:

我可能误解了这个问题。也许您想要 10 % ((@Number / 10) + @Number)

(@Number / 10) + @Number 是两位数的数字之和。

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.

风轻花落早 2024-08-19 18:47:46

如果数字始终为两位数,那么我将使用 LEFT(@Number,1)RIGHT(@Number,1) 来访问每个数字。如果不是,请回复评论,我将为您提供一些有关您需要的循环的帮助。

更紧迫的问题是,您期望的是 2,但 8 mod 10 的结果是 8。如果您要查找 2,则总和为 10 mod 8 (10 % @Result)

无论如何,如果这不能完全满足您的要求,请给我回信。

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.

皓月长歌 2024-08-19 18:47:46

试试这个

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

希望这有帮助

如果你只想添加 2 个数字试试这个(尽管它已经在上面实现了)

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

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
千仐 2024-08-19 18:47:46

所以我猜你想要这样的东西 - 解析代表你的号码的字符串,将各个数字加起来作为整数值。这最终会给你一个总结果——然后你可以用它做任何你需要做的事情。此代码适用于任何长度的字符串(最多 50 个字符 = 原始号码中的 50 个数字):

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'

@Number 的初始值为“62”,我得到的结果为 8 - 现在您可以继续使用该值。

如果您经常需要此函数,我可能会将其封装到用户定义的函数中,以便您可以从代码中的任何位置调用它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文