crackyourinterview.com


To improves our performance please Like Share Subscribe(Will boost us)

Create a random password in sql
Question Posted on 13 Jun 2017Home >> DataBase >> SQL Query >> Create a random password in sql

To create a randome password i have created 2 function and one view to get complex password below is the code for the same

(1)First part is view

CREATE VIEW Get_RAND
AS
SELECT RAND() AS MyRAND
GO



(2)Second part is to get random number in any range define in it

CREATE FUNCTION RANDBETWEEN(@LowerBound INT, @UpperBound INT)
RETURNS INT
AS
BEGIN
DECLARE @TMP FLOAT;
SELECT @TMP = (SELECT MyRAND FROM Get_RAND);
RETURN CAST(@TMP* (@UpperBound - @LowerBound) + @LowerBound AS INT);
END
GO


(3)Third is to create a 10 digit password with different keywords and syntax

alter FUNCTION dbo.GeneratePassword ()
RETURNS varchar(10)
AS
BEGIN
DECLARE @randInt int;
DECLARE @NewCharacter varchar(1);
DECLARE @NewPassword varchar(10);
SET @NewPassword='';

--6 random characters
WHILE (LEN(@NewPassword) <6)
BEGIN
select @randInt=dbo.RANDBETWEEN(48,122)
IF @randInt<=57 OR (@randInt>=60 AND @randInt<=93) OR (@randInt>=97 AND @randInt<=122)
Begin
select @NewCharacter=CHAR(@randInt)
select @NewPassword=CONCAT(@NewPassword, @NewCharacter)
END
END

--Ensure a lowercase
select @NewCharacter=CHAR(dbo.randbetween(97,122))
select @NewPassword=CONCAT(@NewPassword, @NewCharacter)

--Ensure an upper case
select @NewCharacter=CHAR(dbo.randbetween(65,90))
select @NewPassword=CONCAT(@NewPassword, @NewCharacter)

--Ensure a number
select @NewCharacter=CHAR(dbo.randbetween(48,57))
select @NewPassword=CONCAT(@NewPassword, @NewCharacter)

--Ensure a symbol
WHILE (LEN(@NewPassword) <10)
BEGIN
select @randInt=dbo.randbetween(33,64)
-- ! # $ % & < = > ? @
IF @randInt=33 OR (@randInt>=35 AND @randInt<=38) OR (@randInt>=60 AND @randInt<=64)
Begin
select @NewCharacter=CHAR(@randInt)
select @NewPassword=CONCAT(@NewPassword, @NewCharacter)
END
END

RETURN(@NewPassword);
END;
GO


And to creata password after executing the above code we need only to call

select GeneratePassword()
0
0



.


Most Visited Questions:-

Deep Learning Questions Answers
Below are the different Deep Leaning Questions and answer a More...

Continuous Integration Questions Answers
Below are the 20 odd questions for CI or Continuous Integra More...

Derived relationships in Association Rule Mining are represented in the form of __________.
Derived relationships in Association Rule Mining are repres More...

What is Gulpjs and some multiple choice questions on Gulp
Gulpjs is an open source whihc helps in building Javascript More...

Microservices Architecture Questions Answers
Below are the different questions on Microservices Architec More...




Other Important Questions

How to get the table count in last used query from cache by sending tablename?

Write a SQL query to print 1 to 100 in sql server?

Sql Interview Latest Query questions asked on 30 dec 2020

nth highest number from table

some good sql query






@2014-2022 Crackyourinterview (All rights reserved)
Privacy Policy - Disclaimer - Sitemap