Let's say, hypothetically, that you need a bit of code to create a unique key in a database table that starts with an "N" and it MUST fit within the limits defined by a varchar(20) column.

You might think that transforming a database sequence ID might be a good way to go, but no, your project manager demands something more...robust. More unique. What to do? Well, Jay sent in the obvious answer: Use a GUID!

Wait...you can't use the whole thing. Ok, just use part of a GUID!

LEFT('N' + CAST(ABS(CAST(CAST(NEWID() AS VARBINARY(5)) AS Bigint)) AS VARCHAR(14)), 19)

...and if you keep it down to 13 characters, like the original developer of this code did, you're doing what developers like to call planning for the future.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!