A function or SQL statement is used to randomly generate any 6 characters.

Source: Internet
Author: User

1.

Declare @ SQL nvarchar (400)
Select @ SQL = 'select char ('+ convert (nvarchar, convert (INT, 26 * rand () + 97) +') +
Char ('+ convert (nvarchar, convert (INT, Rand () * 26) + 97) +') +
Char ('+ convert (nvarchar, convert (INT, Rand () * 26) + 97) +') +
Char ('+ convert (nvarchar, convert (INT, Rand () * 26) + 97) +') +
Char ('+ convert (nvarchar, convert (INT, Rand () * 26) + 97) +') +
Char ('+ convert (nvarchar, convert (INT, Rand () * 26) + 97) + ')'
Print @ SQL
Exec (@ SQL)

 

========================================================== ==================

2.

Declare @ STR varchar (10)
Set @ STR =''
Select @ STR = @ STR + CHR from
(Select top 6 CHR from (
Select 'A' as CHR Union all
Select 'B' Union all
Select 'C' Union all
Select 'D' Union all
Select 'E' Union all
Select 'F' Union all
Select 'G' Union all
Select 'H' Union all
Select 'I' Union all
Select 'J' Union all
Select 'K' Union all
Select 'l' Union all
Select 'M' Union all
Select 'n' Union all
Select 'O' Union all
Select 'P' Union all
Select 'q' Union all
Select 'R' Union all
Select's 'Union all
Select 't'union all
Select 'U' Union all
Select 'V' Union all
Select 'W' Union all
Select 'x' Union all
Select 'y' Union all
Select 'Z'
) As a order by newid () as t

---- View
Select @ Str

 

========================================================== =

3.

Declare @ myid varchar (1000)
Set @ myid = newid ()
Select convert (varchar (6), @ myid)

 

========================================================== =

4.

Declare @ below int
Declare @ Up int
Select @ below = 65, @ up = 90
Select char (cast (RAND () * (@ up-@ below) + @ below as decimal (38,0 )))
+ Char (cast (RAND () * (@ up-@ below) + @ below as decimal (38,0 )))
+ Char (cast (RAND () * (@ up-@ below) + @ below as decimal (38,0 )))
+ Char (cast (RAND () * (@ up-@ below) + @ below as decimal (38,0 )))
+ Char (cast (RAND () * (@ up-@ below) + @ below as decimal (38,0 )))
+ Char (cast (RAND () * (@ up-@ below) + @ below as decimal (38,0 )))

 

========================================================== =

5.

Create view v_rand as select rand1 = convert (INT, Rand () * 26), rand2 = rand () * 2
Go

Create Function DBO. f_getrandnum (@ Len int, @ flag INT)
Returns nvarchar (100)
As
-- @ Len: length of output characters
-- @ Flag return value contains character 1: uppercase letters 2: lowercase letters 3: uppercase and lowercase letters mixed
Begin
Declare @ SQL nvarchar (100), @ Rand int
Select @ SQL =''
If @ Len> 100
Set @ Len = 100

While @ Len> 0
Begin
Select @ Rand = rand1 + (Case @ flag when 1 then 65 when 2 then 97
Else (case when rand2> 1 then 97 else 65 end)
From v_rand

Select @ SQL = @ SQL + char (@ rand), @ Len = @ len-1
End

Return @ SQL
End
Go

Select DBO. f_getrandnum (7,3)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.