Quantcast
Viewing latest article 6
Browse Latest Browse All 8

Random Data – Part 2

A few years ago I found this procedure on SQLServerCentral – I believe (but I can’t find it today) – it creates a random password with options for upper case, lower case, special characters and numbers.  I liked this procedure as I’m kind of a security guy and I use it for all my random password needs.  Note, this was written and tested for SQL Server version 7.0 and 2000 – a good thing lasts.  Anyway, recently I’ve been doing some testing and in need of creating test data, so, not only did I create this script to populate a table, I’ve now been tweaking the password generator procedure.

Here’s the basic’s of it.

Select CHAR(ROUND(97 + (RAND() * (25)),0)) as 'Lowercase',
	CHAR(ROUND(65 + (RAND() * (25)),0)) as 'Uppercase',
	CHAR(ROUND(48 + (RAND() * (9)),0)) as 'Number',
	CHAR(ROUND(33 + (RAND() * (13)),0)) as 'SpecialCharacter'

For Lower Case letters, here’s how it works:

  1. The RAND() function randomly picks a float from 0 to 1
  2. That random float is multiplied by 25, so it could be any number 0 through 25 – note 26 integers
  3. Add 97 to that, so the number now could be 97 through 122.
  4. The ROUND() function rounds that number to the nearest integer as it’s passing in a 0 for length — ROUND(<<Num>>, 0), so we’ve got integers 97 though 122.
  5. Finally, those integers are passed into the CHAR function, which converts an integer to a character.

The same idea’s apply to the upper case – except integers ranging from 65 to 90, etc.

Here’s a chart for the integer ASCII code to a characters

Integers Characters
97 – 122 Lower case
65 – 90 Upper case
48 – 57 Numbers 0 – 9
33 – 46 Characters !”#$%&’()*+,-.

*Note the Char(10) is a line feed and Char(13) is a carriage return

Here’s a script to see all the integer ASCII code to a characters

Declare @TTable table (Number int, Character varchar(5))
Declare @TData int
Set @TData = 33 

While @TData < 256
Begin
    Insert @TTable Select @TData, CHAR(@TData)
    Set @TData = @TData + 1
End 

Select * from @TTable

Here’s a good example that uses this idea to create a view of a fixed length column – kind of a brute force method, but sometimes when your in a bind, it might just be crazy enough to work.


Viewing latest article 6
Browse Latest Browse All 8

Trending Articles