Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
How to generate a random number with 23 digits in sql?


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
Posted
Updated 21-Apr-16 6:55am
Comments
Patrice T 21-Apr-16 9:05am    
Can you tell us why you need 23 digits ?
ranio 21-Apr-16 9:40am    
it's an account no: field which must have min 23 digits and it must be a unique number too
Tomas Takac 21-Apr-16 10:01am    
Does it need to be random? If not you could use a sequence. Provided you are on SQL Server 2012 or newer.
CHill60 22-Apr-16 5:20am    
You've been given a couple of solutions to generate the random 23 characters - but how are you going to guarantee uniqueness?

try this

SQL
select  left (   ( CONVERT(decimal(30,0) ,  ( (( RAND() *999999999999 )/.00012345671) *999999999  ))) , 23 )
 
Share this answer
 
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'));


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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900