SQL generates n-bit random string

Source: Internet
Author: User

--1, with newid () go-Create a view (because it cannot be used directly in the function for NEWID ()) CREATE View Vnewid as select NEWID () N ' macoid '; Go--creating functions Create function Getrandstr (@n int) returns varchar (max) as BEGIN declare @i int set @i=ceiling (@n/ 32.00) declare @j int set @j=0 declare @k varchar (max) set @k= "while @j<@i begin s      Elect @[email Protected]+replace (CAST (macoid as varchar), '-', ') from Vnewid set @[email protected]+1 end Set @k=substring (@k,1,@n) return @k End-Test Demo sample Select DBO.GETRANDSTR (75)-Execution result/* d185504ad09c4d5796f7016983 E67414CEE25162EA9F43D195D43328A4CF01AC7C586521D8E */-we can find that the letters in the results are in uppercase. Or it's all lowercase.

--In a different way to write: Go--Create the function "creation" functions [dbo]. [M_rand] (@mycount int) returns nvarchar (a) as BEGIN DECLARE @maco_wang table (ID varchar (1)) Declare @maco_n umber int, @number int; declare @my_one nvarchar (max), @my_two nvarchar (max) Set @my_one = "; set @maco_number = 0; Set @number = 48; while (@number >=48 and @number <=57) or (@number >=65 and @number <=90) or (@number >=97 and @number <=122) Begin INSERT INTO @maco_wang Select char (@number) Set @[email protected]+1; if (@number =58) begin set @number =65 End if (@number =91) Begin set @number =97 End while @maco_number < @mycount Begin select @my_two =id from @maco_wang order by (select Macoid from Dbo.m_macoview); Set @[email protected][email protected]_one; Set @[email protected]_number+1; End return @my_one End--Test Case SELECT [dbo]. [M_rand] (75)--Results of */5NN0W4O4VOKJACB5SO2UVCUW2ZRRNBHXEI4ICSEOHZBBSTKMR1P8ASH4N4XAXHDODETKX8BZ0CR */


SQL generates n-bit random string

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.