[Webinar] Streamline your web hosting managementRegister Today

x
Solved

# How to write SQL Syntax that generate random number of 13 digits?

Posted on 2011-10-13
Medium Priority
294 Views
Hello,

How to write SQL Syntax that generate random number of 13 digits?

Thanks

0
Question by:zaki100

LVL 71

Assisted Solution

Éric Moreau earned 1000 total points
ID: 36962403
you can use the SP found at http://www.brettb.com/SQL_Help_Random_Strings.asp and set the @ValidCharacters only digits only
0

LVL 32

Accepted Solution

awking00 earned 1000 total points
ID: 36962412
declare @Random int;
declare @Upper int;
declare @Lower int;

set @Lower = 1000000000000
set @Upper = 9999999999999
select @Random = ROUND(((@Upper - @Lower - 1) * RAND() + @Lower), 0)
select @Random
0

LVL 8

Expert Comment

ID: 36962415
Hi Zaki,

Could you please explain your needs in details? where will you use this random numbers? the idea is to make it on the script (PHP, ASP, VB...) and then insert the result on the database using normal sql syntax.

Other wise if you are using stored procedures in the SQL server side.

so could you please explain so you can gave a better answer?

BR
0

LVL 71

Expert Comment

ID: 36962447
selmrabet, your code can work but only if you replace int with BIGINT

declare @Random bigint;
declare @Upper bigint;
declare @Lower bigint;
0

LVL 71

Expert Comment

ID: 36962455
I meant awking00
0

LVL 32

Expert Comment

ID: 36962565
selmrabet,
Thanks for pointing that out. I usually don't have a need to generate random numbers so large :-)
0

LVL 32

Expert Comment

ID: 36962576
And I meant emoreau! Sorry for any confusion.
0

LVL 72

Expert Comment

ID: 37218527
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.
###### Suggested Courses
Course of the Month9 days, 9 hours left to enroll