In this post explains how to use the RAND function in SQL Server (Transact-SQL) with syntax and examples.
In SQL Server (Transact-SQL), the RAND function can be used to return a random number or a random number within a range.
The syntax for the RAND function in SQL Server (Transact-SQL) is:
Optional. If specified, it will produce a repeatable sequence of random numbers each time that seed value is provided.
To create a random decimal number between two values (range), you can use the following formula:
Where a is the smallest number and b is the largest number that you want to generate a random number for.
The formula above would generate a random decimal number between 10 and 25, not inclusive.
To create a random integer number between two values (range), you can use the following formula:
Where a is the smallest number and b is the largest number that you want to generate a random number for.
The formula above would generate a random integer number between 10 and 25, inclusive.
The RAND function can be used in the following versions of SQL Server (Transact-SQL):
Let's explore how to use the RAND function in SQL Server (Transact-SQL) to generate a random number between 0 and 1, not inclusive.
For example:
Let's explore how to use the RAND function in SQL Server (Transact-SQL) to generate a random decimal number between two numbers (ie: range).
For example, the following would generate a random decimal value between 1 and 10, not inclusive (random number would be greater than 1 and less than 10):
Let's explore how to use the RAND function in SQL Server (Transact-SQL) to generate a random integer number between two numbers (ie: range).
For example, the following would generate a random integer value between 10 and 20, inclusive: