如何在 SQL Server 2000 中将 2 位数字相加
假设我有一个数字 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
或者使用
left
和right
访问子字符串。Or use
left
andright
to access substrings.我没有办法尝试:
I have no way to try it:
我认为
@Number % 10
就是您正在寻找的。它返回任何数字的最后一位数字。 62-> 2、 97 --> 7等...更新:
我可能误解了这个问题。也许您想要
10 % ((@Number / 10) + @Number)
。(@Number / 10) + @Number
是两位数的数字之和。I think
@Number % 10
is 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.如果数字始终为两位数,那么我将使用
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)
andRIGHT(@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.
试试这个
希望这有帮助
如果你只想添加 2 个数字试试这个(尽管它已经在上面实现了)
Try this
Hope this helps
And if u just want to add 2 numbers try this(though it has been implemented above)
所以我猜你想要这样的东西 - 解析代表你的号码的字符串,将各个数字加起来作为整数值。这最终会给你一个总结果——然后你可以用它做任何你需要做的事情。此代码适用于任何长度的字符串(最多 50 个字符 = 原始号码中的 50 个数字):
@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):
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.