Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
pdude1978
Starting Member
3 Posts |
Posted - 2011-03-21 : 12:23:57
|
| Hello EveryoneI'm trying to write a function that would generation a random string from A-Z, it needs to include letters in uppercase and lowercase each time for e.g. ‘DgJkMc’. I have come across the following code that works perfectly giving random letters each time but not in the format I need.Any help would be appreciated Thanks GuysDECLARE @length int = 5DECLARE @RandomID varchar(32) DECLARE @counter smallint DECLARE @RandomNumber float DECLARE @RandomNumberInt tinyint DECLARE @CurrentCharacter varchar(1) DECLARE @ValidCharacters varchar(255) DECLARE @ValidCharactersLength int SET @ValidCharacters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' SET @ValidCharactersLength = len(@ValidCharacters) SET @CurrentCharacter = '' SET @RandomNumber = 0 SET @RandomNumberInt = 0 SET @RandomID = '' SET @counter = 1 WHILE @counter < (@Length + 1) BEGIN SET @RandomNumber = Rand() SET @RandomNumberInt = Convert(tinyint, ((@ValidCharactersLength - 1) * @RandomNumber + 1)) SELECT @CurrentCharacter = SUBSTRING(@ValidCharacters, @RandomNumberInt, 1) SET @counter = @counter + 1 SET @RandomID = @RandomID + @CurrentCharacterEND SELECT @RandomID AS 'ActivationCode' |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-03-21 : 12:57:05
|
| declare @password varchar(8)set @password=''select @password=@password+char(n) from( select top 8 number as n from master..spt_values where type='p' and (number between 65 and 90 or number between 97 and 122 or number between 48 and 57) order by newid()) as tselect @passwordJimEveryday I learn something that somebody else already knew |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
pdude1978
Starting Member
3 Posts |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2011-03-22 : 04:17:43
|
| Have you tied jim's code?MadhivananFailing to plan is Planning to fail |
 |
|
|
pdude1978
Starting Member
3 Posts |
Posted - 2011-03-22 : 06:52:45
|
Hi Jimthanks for your help it works perfectly quote: Originally posted by jimf declare @password varchar(8)set @password=''select @password=@password+char(n) from( select top 8 number as n from master..spt_values where type='p' and (number between 65 and 90 or number between 97 and 122 or number between 48 and 57) order by newid()) as tselect @passwordJimEveryday I learn something that somebody else already knew
|
 |
|
|
|
|
|