/* ------------------------ My Meta Content Here SEO ------------------------ */

Pages

Main Menu

Wednesday, May 21, 2014

Using sql server generating random number

Using sql server generating random number

SELECT randomNumber
FROM (SELECT ABS(CAST(NEWID() AS binary(6)) %50) + 1 randomNumber FROM sysobjects) sample GROUP BY randomNumber
--ORDER BY randomNumber

Declare @Random varchar(10)
set @Random = CONVERT(varchar(10), right(newid(),10))
SELECT TOP 50 randomNumber
FROM (SELECT CAST(ABS(CAST(NEWID() AS binary(6)) %50) + 1 AS VARCHAR(10)) + CONVERT(varchar(10), right(newid(),10)) randomNumber
FROM sysobjects) sample
GROUP BY randomNumber

SELECT 20*RAND()   

----Method 1 : Generate Random Numbers (Int) between Rang
------ Create the variables for the random number generation
--DECLARE @Random INT
--DECLARE @Upper INT
--DECLARE @Lower INT

------ This will create a random number between 1 and 999
--SET @Lower = 1 ---- The lowest random number
--SET @Upper = 50 ---- The highest random number
--SELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
--SELECT @Random

No comments:

Post a Comment

My Blog List