sqlsql-serverdatabasesql-server-2008newid

Avoid certain characters in NEWID() in SQL


I have a table with columns id and name. In the id column, a key generated using NEWID() is inserted.

SET @myid = NEWID()
SELECT @id = SUBSTRING(CONVERT(varchar(255), @myid), LEN(@myid)-5, LEN(@myid))

This could be some randomly generated alphanumeric key, but I want to avoid the letter O and number 0 from this key. A simple replace of O and 0 will not work. Is there any other method to do this?


Solution

  • The NEWID() function produces a UNIQUEIDENTIFIER value AKA a GUID. You have no control over what the value is, nor should it matter. If you need to control it, than you are using it wrong. It's not just a randomly-generated string of characters (I would suggest you read about GUID at the link I provided).

    The GUID value consists of hexadecimal characters which is 0-9 and A-F. Therefore, you would never have the letter O.