List of First Names and Last Names for Test Data, Plus a Query to Generate a Random Full Name

I found this page to be very helpful when I was generating random names for test data:

http://www.quietaffiliate.com/free-first-name-and-last-name-databases-csv-and-sql/

Naturally, I didn't read everything and just grabbed the two CSV files (one for first names, a second for last names). Next, I created two tables (FirstNames, LastNames) and imported the CSV files into their respective tables.

The following query will assemble a Full Name from a randomly selected First Name and randomly selected Last Name:


-- Generate random LastNameID
DECLARE @RandomLastNameID int;
DECLARE @UpperLastNameID int;
DECLARE @LowerLastNameID int;
SET @UpperLastNameID = 88799;
SET @LowerLastNameID = 1;
SET @RandomLastNameID = RAND() * @UpperLastNameID + @LowerLastNameID;

-- Generate random FirstNameID
DECLARE @RandomFirstNameID int;
DECLARE @UpperFirstNameID int;
DECLARE @LowerFirstNameID int;
SET @UpperFirstNameID = 5494;
SET @LowerFirstNameID = 1;
SET @RandomFirstNameID = RAND() * @UpperFirstNameID + @LowerFirstNameID;

-- Generate FullName from random first & last name IDs
SELECT
(SELECT
FirstNames.Name AS FName
FROM
FirstNames
WHERE
FirstNames.FirstNameID = @RandomFirstNameID)
+ ' ' + 
(SELECT
LastNames.Name AS LName
FROM
LastNames
WHERE
LastNames.LastNameID = @RandomLastNameID) AS FullName


Running this query will produce something like:
Lettie Ralon

Running it again will produce yet another name:
Blossom Ciullo

In real life you'd probably want to separate the First Name and Last Name into two columns but my stupidly simply test database just needed a name.




Comments

Popular posts from this blog

Using Reference Aliases

List of Visual Studio Keyboard Shortcuts