Amazon Ad

Wednesday, 26 November 2014

Generate Verification Code In MS SQL

Hi Guys,

I was given a task to generate an email verification code from MS SQL. I finally achieved with some  string functions and inbuilt Random function in MS SQL. Here is the code

    --Generate verification code
    declare @ab varchar(36)='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    declare @vercode varchar(11)=''
    declare @counter int=0
    declare @ind int=0
    WHILE @counter < 9
    BEGIN
        set @ind=(select RAND()*35)
        set @vercode=@vercode+(select SUBSTRING(@ab, @ind+1, 1))
        SET @counter = @counter + 1
    END
    print @vercode

The below code generates an alphanumeric code having 9 alphanumeric characters. You can increase or decrease the length by chaning the lenght of the @vercode variable and the while loop counter.

Thanks
Ritesh

No comments:

Post a Comment

Comments are welcome, Please join me on my Linked In account

http://in.linkedin.com/pub/ritesh-tandon/21/644/33b

How to implement Captcha v3 in ASP.NET

 I was facing an issue of dom parsing in my website. I finally resolved it by using Google Captcha V3. Step 1: Get your keys from https:...