Generating Random Numbers - Part II

There are many methods to generate random numbers. Here are some of them


select number from

(

select top 10000 abs(checksum(newid())) as number

from sysobjects s1 cross join sysobjects s2

) as T

select number from

(

select top 1000 abs(cast(cast(newid() as varbinary(10)) as int)) as number

from sysobjects s1 cross join sysobjects s2

) as T

 

If you want to generate 4 digits random number, then use

 

select number/power(10,len(number)-4) as number from

(

select top 10000 abs(checksum(newid())) as number

from sysobjects s1 cross join sysobjects s2

) as T

If you want to generate 2 digits random number, then use

select number/power(10,len(number)-2) as number from

(

select top 10000 abs(checksum(newid())) as number

from sysobjects s1 cross join sysobjects s2

) as T

If you use SQL Server 2005, you can also apply the same approach on the following query

 

select row_number() over (order by s1.name) as number

from sysobjects s1 cross join sysobjects s2

 

Published 10 October 2007 09:10 by Madhivanan

Comments

# Another use of GO command in SQL Server 2005

06 August 2008 10:01 by Madhivanan

As you all know, GO command signals the end of the batch of T-SQL statements However in SQL Server 2005

# Another use of GO command in SQL Server 2005

06 August 2008 10:10 by SQL Server Transact-SQL (SSQA.net)

As you all know, GO command signals the end of the batch of T-SQL statements However in SQL Server 2005,