如何在SQL中增加23位数? [英] How to increment 23 digit number in SQL?

查看:112
本文介绍了如何在SQL中增加23位数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在sql中增加23位数字?



我尝试使用下面的代码:

How to increment 23 digit number in sql?

I tried using below code:

--88800021032013000000808
declare @i int
set @i=1
while @i<=20000
begin
Declare @MandateRef VARCHAR(23)
Declare @staticMandateRef VARCHAR(23)

set @staticMandateRef='88800021032013000000808'

--set @MandateRef=substring(@staticMandateRef,1,20)+CONVERT(VARCHAR(10),CONVERT(INT,RIGHT(RTRIM(@staticMandateRef),3))+@i)





print @MandateRef

PRINT @i
set @i=@i+1

end





但它是重复的在191条记录之后。



But it is duplicating after 191 records.

推荐答案

基本上,你不能以任何简单的方式使用整数。

SQL INT是32位,所以a最大值2,147,483,647:10位数。

SQL BIGINT是64位,因此最大值为9,223,372,036,854,775,807:19位数。

增加23位数意味着你必须写字节或基于字符串的数字系统函数来设置,增加和转换字符串 - 这不是不可能的,但它很麻烦,因为你必须一次添加一个数字并将结果携带到下一个数字。



另一种方法是尝试使用支持高达38位精度的DECIMAL数字 - 但是你不能将它们转换为整数来输出它们,只有DECIMAL或NVARCHAR。



基本上,SQL没有23位整数。我?我会看看为什么我需要这个并试图看看是否有更好的方法的设计原因......
Basically, you can't use integers in any simple way.
SQL INT is 32 bits, so a max of 2,147,483,647 : 10 digits.
SQL BIGINT is 64 bits, so a max of 9,223,372,036,854,775,807 : 19 digits.
To increment 23 digit numbers means you would have to write byte or string based "number system" functions to set, increment, and convert to string - which isn't impossible, but it's messy as you will have to add a digit at a time and "carry" the result up to the next digit.

The alternative is to try using DECIMAL numbers which do support up to 38 digit precision - but you can't convert those to integer to output them, only DECIMAL or NVARCHAR.

Basically, SQL doesn't have 23 digit integers. Me? I'd be looking at the design reason why I needed this and trying to see if there was a better way...


这篇关于如何在SQL中增加23位数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆