How to generate alphanumeric random numbers using a function in SQL Server 2008

I need to create an alphanumeric random number with a length of 6 characters, and it must contain Numerics, Alphabets (both lower and upper case) to check the query below.

I NEED FOR IMPLEMENTATION OF FUNCTION. (You can use NEWID (), RAND () in the function).

SELECT SUBSTRING(CONVERT(VARCHAR(255), NEWID()),0,7)

Conclusion:

23647D
06ABA9
542191
.
.
.

I need an output like:

236m7D
3n64iD
6t4M7D
.
.
.
+4
source share
3 answers

As in the function, we cannot use NEWID () OR RAND (), first we need to create a VIEW

For function

CREATE VIEW NewID as select newid() as new_id


DECLARE @new_id VARCHAR(255)


SELECT @new_id = new_id FROM newid


SELECT @Password = CAST((ABS(CHECKSUM(@new_id))%10) AS VARCHAR(1)) + 
CHAR(ASCII('a')+(ABS(CHECKSUM(@new_id))%25)) +
CHAR(ASCII('A')+(ABS(CHECKSUM(@new_id))%25)) +
LEFT(@new_id,3)


SELECT @PASSWORD

Conclusion:

9eEF44
5uUFA2
7hHFA7
.
.
.

For Select statement

DECLARE @new_id VARCHAR(200)

SELECT @new_id = NEWID()

SELECT CAST((ABS(CHECKSUM(@new_id))%10) AS VARCHAR(1)) + 
CHAR(ASCII('a')+(ABS(CHECKSUM(@new_id))%25)) +
CHAR(ASCII('A')+(ABS(CHECKSUM(@new_id))%25)) +
LEFT(@new_id,3)

Conclusion:

0aAF3C
5pP3CE
2wW85E
.
.
.
+4
source

Try the following:

 select cast((Abs(Checksum(NewId()))%10) as varchar(1)) + 
       char(ascii('a')+(Abs(Checksum(NewId()))%25)) +
       char(ascii('A')+(Abs(Checksum(NewId()))%25)) +
       left(newid(),5) Random_Number

Besides,

    DECLARE @exclude varchar(50) 
    SET @exclude = '0:;<=>?@O[]`^\/'
    DECLARE @char char
    DECLARE @len char
    DECLARE @output varchar(50)
    set @output = ''
    set @len = 8

    while @len > 0 begin
       select @char = char(round(rand() * 74 + 48, 0))
       if charindex(@char, @exclude) = 0 begin
           set @output = @output + @char
           set @len = @len - 1
       end
    end

   SELECT @output

.

+4

.

create view NewID  as select newid() as [newid] , RAND() as [rand]


CREATE FUNCTION [dbo].[GenerateRandomID]
(
@length as int
)
RETURNS   VARCHAR(32)
AS
BEGIN
declare @randIndex as int 
declare @randstring  as varchar(36)

select  @randIndex = CEILING( (30 - @length) * [rand]) , @randstring=   Replace(CONVERT (varchar(40) , [newid]) , '-','') from getNewID

-- Return the result of the function
RETURN SUBSTRING(@randstring,@randIndex, @length)

END

As you generate a GUID, you can generate a random number.

+2
source

All Articles