Web design and development with excellence

Random Numbers in MS SQL

By manik26 • Oct 20th, 2009 • Category: Blog

Lets say we want to have a random integer number between 1 to 2 using MS SQL. How can we do that?

Easy, using the RAND function:

SELECT CEILING(2*RAND());

RAND function returns a float value from 0 to 1 exclusive. You must have noticed the word exclusive in bold.

Note that using the seed in RAND produces the same result over and over. So using seed wasnt a solution.