Marcelo Assis
Marcelo Assis

Reputation: 5214

How can I get a random value from a range of numbers?

Foremost, I saw this question before posting, mine is not a duplicate, our questions are different beside the titles.

I don't have a big knowledge in SQLSERVER/T-SQL, so when I was "translating" some scripts from ORACLE/PLSQL, I didn't found any function to generate a random number between 2 values.

In the PLSQL script I'm looking at, I can get it simply using DBMS_RANDOM.VALUE(5, 10).

How can I do that in T-SQL? Thanks!

Upvotes: 0

Views: 1592

Answers (1)

Guffa
Guffa

Reputation: 700870

You would use the rand function, which gives you a value 0 <= n < 1. Example for a number between 5 and 10 (inclusive):

floor(rand() * (10 - 5 + 1)) + 5

Upvotes: 1

Related Questions