I need to generate random values for multiple rows in a column. The random values need to be in the range of 0.005 and 0.015 inclusive.
I have tried using the abs(checksum(newID()) but I am not sure how to specify a range using that.
You can give a range like this by using the RAND() function.
Please execute SQL and you can see. The number that comes will be within the range.
SQL:
SELECT RAND()*(0.015-0.005)+0.005 as randomNumber
Here, 0.015 and 0.005 are range number.
SQL output
|----|-------|----------------------|
|id |empCode| randomNumber |
|----|------------------------------|
|1 | 0001 | 0.01245219297469798 |
|----|-------|----------------------|
|2 | 0002 | 0.010188965811570142 |
|----|-------|----------------------|
Hopefully, your problem will be resolved.
Late to the game... but here's a good solution: Random number in range.
ABS(CHECKSUM(NEWID()))%<Number of Values in Range> + <Lowest Value in Range> + 1
So here we get a RandomNumber for each row with values between 10 and 20
SELECT FirstName,MiddleName,LastName, ABS(CHECKSUM(NEWID()))%10 + 11 AS RandomNumber
FROM Person.Person