How to get random number in sql server

RAND() function in sql returns random decimal number. I need interger value that's why i am applying multiply to get integer number. I used LEFT() to get only 6 digit number.
You can increase or decrease number according to your requirements.
SELECT LEFT(CAST(RAND() * 1000000000 AS INT), 6)
--Output: 842870
To get 4 digit random number, see below example.
SELECT LEFT(CAST(RAND() * 1000000000 AS INT), 4)
--Output: 3445

 Posted Comments

No comments have been posted to this article.

 Post a comment

Name:
Email:
Comment:
Security Code:
58 + 40
=
We don't publish your email on our website.