COGNOiSe.com - The IBM Cognos Community

IBM Cognos 8 Platform => COGNOS 8 => Report Studio => Topic started by: Prakash Bhai on 04 May 2010 01:15:48 AM

Title: RandBetween
Post by: Prakash Bhai on 04 May 2010 01:15:48 AM
Hi All,

Is there any Randbetween function kind of thing in Cognos.


Regards

Abhi

Title: Re: RandBetween
Post by: CognosPaul on 04 May 2010 04:57:03 AM
There is no native randbetween in Cognos, but you can use database functions for this.

First question, which database are you using? My examples will only be applicable for SQL Server 2005.

If you need a single random value for the entire list you can use #prompt('Low','integer')# + { RAND()} * (#prompt('High','integer')#-#prompt('Low','integer')#) Note the passthrough SQL brackets.

If you need a random value for each row returned you can use the slightly more complex #prompt('Low','integer')# + { RAND(CAST(NEWID() AS BINARY(6)))} * (#prompt('High','integer')#-#prompt('Low','integer')#) If you run it against a long list you'll notice the results aren't 100% random though.

The caveat is that it needs to be in a query being run against the database. You'll get an error if you try to run that alone in a query.

Title: Re: RandBetween
Post by: Prakash Bhai on 04 May 2010 05:58:07 AM
Hi Paul,

Thx for the reply. Well i am using DB2 as my DB. Can i use the same functionality for DB2 also r it is only applicable for SQL....

Regards
Abhi

Title: Re: RandBetween
Post by: CognosPaul on 04 May 2010 06:12:45 AM
I'm not familiar at all with DB2. A quick google search shows there is a RAND function.

Try using #prompt('Low','integer')# + RAND() * (#prompt('High','integer')#-#prompt('Low','integer')#) in a data item. The logic is simple: ((random number * (High - Low) ) + Low) will act as randbetween. The hard part is just finding the correct DB2 function to return the random number.