Wednesday, July 29, 2015

RANDOM Function in SQL Server



/*
Understanding Random function in SQL Server

Repetitive calls of RAND() with the same seed value return the same results.

For one connection, if RAND() is called with a specified seed value, all subsequent calls of RAND() produce results based on the seeded RAND() call. For example, the following query will always return the same sequence of numbers.

*/

SELECT RAND(100), RAND(), RAND()

begin
select rand()
end

begin
select rand()
end

/* union all example for Random Function */
SELECT
        RAND(100)
    ,   RAND(7)
    ,   RAND()
 UNION ALL
 SELECT
        RAND(100)
    ,   RAND(7)
    ,   RAND()
 UNION ALL
 SELECT
        RAND(100)
    ,   RAND(7)
    ,   RAND()


/* This query output will retur three rows */

No comments:

Post a Comment