Come utilizzare la funzione SQL RAND
The RAND() SQL function returns a float random number.
Many times is helpful to have an integer random number, inside a range of values, between a minumum and maximum numbers.
The following snippet shows hw to use the SQL random RAND function:
DECLARE @RdmNum float, @RdmInt int, @Max int, @Min int -- Max value of random int SET @Max = 15000 -- Min value of random int SET @Min = 0 --RAND SQL function SELECT @RdmNum = RAND() SELECT @RdmInt = ((@Max + 1) - @Min) * @RdmNum + @Min SELECT @RdmNum AS RandomNumber, @RdmInt AS RandomInteger
La funzione SQL RAND() restituisce un numero random float.
Spesso è utile avere un numero random che sia intero positivo e che rientri in un intervallo di valori, quindi un random tra un minimo e un massimo.
Di seguito una porzione di codice per l’utilizzo della funzione random RAND SQL:
DECLARE @RdmNum float, @RdmInt int, @Max int, @Min int -- Max value of random int SET @Max = 15000 -- Min value of random int SET @Min = 0 --RAND SQL function SELECT @RdmNum = RAND() SELECT @RdmInt = ((@Max + 1) - @Min) * @RdmNum + @Min SELECT @RdmNum AS RandomNumber, @RdmInt AS RandomInteger
1 commento