August 31, 2016 at 1:07 am
Hi all,
I am working on Sql Server 2008 and I have a requirement that for a specific column I have to populate a value between 0.25-6.5. All the values should be in this range. I have 65K records in these values should be distributed.
Please let me know if there is any way. I have to update this value.
Thanks in advance.
Ammy
August 31, 2016 at 1:42 am
Have a look at the code bellow. I think that this is what you are looking for.
create table #t (dc decimal (10,9))
go
insert into #t (dc)
select 6.25 * rand() + 0.25
go 65000
select min(dc), max(dc), avg(dc)
from #t
Adi
--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
August 31, 2016 at 2:21 am
Hi Adi,
Really you solved my problem in a very short time.
Thanks a lot.
August 31, 2016 at 7:26 am
After looking at the output from RAND(), you may want to read this article:
http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/sql-server-set-based-random-numbers/[/url]
RAND() values repeat when doing set-based operations, so many of us use the following formula to generate random numbers.
Abs(Checksum(NewId())) % 10
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
August 31, 2016 at 8:54 am
Here's an article on generating random integers and floats. The best part is that it doesn't have to go row by row.
http://www.sqlservercentral.com/articles/Data+Generation/87901/
DECLARE @Range decimal(5,2) = 6.5-.25, @StartValue decimal(5,2) = .25;
SELECT SomeRandomFloat = RAND(CHECKSUM(NEWID())) * @Range + @StartValue
FROM sys.all_columns a, sys.all_columns b;
September 1, 2016 at 3:12 am
Nice one. Really, We don't need to run one by one...........
Thanks
September 1, 2016 at 11:52 pm
Hi,
All these are generating random values but It's not working for any select statement.
Like
In the select these are generating same random value in the select row for all the rows.
So I found another solution.
select
Value = (select ABS(CAST(NEWID() AS binary(6)) %7) + 0.25 )
from Tablename
It's working fine and generating different values.
Also, There was a change in the requirement that I needed the values in the multiple of .25
like
.25, 0.5, .75 etc till 6.25
Thanks
September 2, 2016 at 5:09 am
ammit.it2006 (9/1/2016)
Also, There was a change in the requirement that I needed the values in the multiple of .25
like
.25, 0.5, .75 etc till 6.25
Thanks
That means you have 25 discrete values.
So use one of the schemes above to generate an "integer" value from 1 to 25, and then divide by 4.
I say "integer" in quotes because while you want integer values, you probably want to store them in a decimal.
September 2, 2016 at 5:40 am
ammit.it2006 (9/1/2016)
Nice one. Really, We don't need to run one by one...........Thanks
This hsould do the trick
Value = ((select ABS(CAST(NEWID() AS binary(6)) %25)) +1) *0.25
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply