如何在SQL中生成23位数的随机数? [英] How to generate a random number with 23 digits in SQL?

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

问题描述

How to generate a random number with 23 digits in sql?





我尝试过兰德()fn。

但不能用23位数生成.. 。



我尝试了什么:



我试过兰德( )fn。

但不能用23位数生成...

FLOOR(RAND(< yourseed>)* 14)+ 1



I tried the RAND() fn.
But not able to generate with 23 digits...

What I have tried:

I tried the RAND() fn.
But not able to generate with 23 digits...
FLOOR( RAND(<yourseed>) * 14) + 1

推荐答案

试试这个



try this

select  left (   ( CONVERT(decimal(30,0) ,  ( (( RAND() *999999999999 )/.00012345671) *999999999  ))) , 23 )


你可以使用一般的解决方案输入一串任意数量的随机数字(或者字符,就此而言)。将以下伪代码调整为SQL语法:



随机字符串'n'字符数:



char数组(n + 1)存储;

存储='';



There's a general solution you can use to create a string of any number of random digits (or characters, for that matter). Adapt the following pseudo-code to SQL syntax:

for a random string of 'n' characters numbers:

char array(n+1) storage;
storage = '';

for(i=0; i< n; i++)
  storage = storage + concatenate( random_value('0' through '9'));





根据您的语言,您可能希望生成0到9的值,并在连接之前添加到'0'字符或为您的值设置范围以便它们生成( ASCII)范围。这个数字很可能需要被转换成一个字符类型来附加任何类型的语言。



回想一下,将23位数值存储为整数很可能是一个整数问题 - 因此它存储为字符串。因此,如果您希望能够按值对它们进行排序,则必须允许前导'0'字符 - 或按长度排序,然后按值排序。



Depending upon your language, you may wish to generate values of 0 through nine and add to the '0' character before concatenation or range your values so that they are generated in that (ASCII) range. The number will likely need to be cast as a character type to append in any typed language.

Recall that storing a 23 digit value as an integer is likely to be a problem - so it's stored as a character string. Because of that, if you wish to be able to sort them by value, you must allow for leading '0' character - or sort by length, then value.


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

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