Sunday, September 7, 2008

Generate random number using t-sql

you can generate random number from ms-sql using Rand() function but you have to use some trick as well.

Here i will create a random password between 1000 - 99999 ;

Check out the code for ms-sql server :

DECLARE @Password int;
DECLARE @Upper int;
DECLARE @Lower int

SET @Lower = 1000;
SET @Upper = 99999
SELECT @Password = Round(((@Upper - @Lower -1) * Rand() + @Lower), 0)

you can put the above code in stored procedure or in a function.
Leave comment if its useful to you
Happy Programming !!

No comments: