Wednesday, 13 March 2013

how to generate random number in sqlserver

DECLARE @chars NCHAR(36)
SET @chars = N'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'

DECLARE @result NCHAR(5)
SET @result = SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1)
            + SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1)
            + SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1)
            + SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1)
            + SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1)

SELECT @result

No comments:

Post a Comment