
Characters besides “-“ are also off the table. The moral is that you can use GUID’s to get random strings, but you only draw from a set of 16 characters, and if you only want to get random letters, you’re restricted to ~23% of the alphabet. This is a problem if you want your random strings to be able to have any character in the Latin alphabet or to have a character distribution that somewhat resembles the distribution in English. So even though your GUID looks like a string and can be converted to a string, it probably cannot contain your name (unless your name is “DEB” or “BEA” or “ABE”). This is because GUIDs are written using hexidecimal digits, and hexidecimal is a numbering system with only 16 numbers (usually the 10 digits plus the first six letters of the alphabet). If you generate enough GUIDs, you will notice that they only seem to contain the numbers 0-9 and the upper-case letters A-F. But the second issue is that GUIDs can only provide a very restricted kind of sample data.
#Random data generator sql code
You might not care about this if you don’t care about unique data or are willing to add code to make sure that a substring of a GUID has not already been used. Raymond Chen has a great blog post that explains how GUIDs are made and why substrings are not necessarily unique. If you are taking substrings of a GUID, these substrings are not guaranteed to be unique. The first issue is somewhat academic but important nonetheless. But there are a few issues with our use of NEWID(), and we should dig a little deeper. That’s pretty cool, and if you’re in a hurry, it will likely do the trick. SELECT CONVERT ( nvarchar ( 10 ), LEFT ( REPLACE ( NEWID (), '-', '' ), 10 )) AS Random10

You may not use this function all that much, since uniqueidentifier columns are usually identity columns and autogenerate on inserts. Fortunately, there is a SQL Server function, NEWID(), that spits out a new GUID (globally unique identifier).

So, if you could generate a bunch of uniqueid’s, you’d have your random strings. It’s also a fun topic, because you don’t need to be working in any particular database we’ll use SQL Server to make the data out of thin air! A First Attempt: NEWID()Īt first glance, there is a datatype that just looks like random letters and numbers: uniqueid. Most importantly, though, it’s an interesting task, because it allows us to explore some of the more uncommon functions in SQL Server.
#Random data generator sql password
Why would you need to make a string of random letters and numbers via SQL? You probably don’t, but you might if you want to load a database with sample data for the purpose of testing queries or if you want to create a temporary password for an account, whose credentials are stored in a SQL database.
