Ok, I found a way of doing this, may not be the best, but it what I thought of: DECLARE @Length INT, @Count INT, @RandNum INT, @RandomString VARCHAR(6), @IsValid BIT SELECT @RandomString = '', @Count = 0, @IsValid = 0, @Length = 6 WHILE (@IsValid = 0) BEGIN WHILE @Count < @Length BEGIN SET @RandNum = SUBSTRING(CAST(Rand() AS VARCHAR(20)), 3, 2) IF (((@RandNum > = ASCII('0')) And (@RandNum <= ASCII('9')) Or (@RandNum > = ASCII('A')) And (@RandNum < = ASCII('Z')))) BEGIN SET @RandomString = @RandomString + CHAR(@RandNum) SET @Count = @Count + 1 END END --Check here that the number is unique IF (SELECT certificateSerialNumber FROM Certificate WHERE certificateSerialNumber = @RandomString) IS Null BEGIN SET @IsValid = 1 END END SELECT @RandomString This cannot be used as a function though, due to the RandNumber generator. You can also emit the last part that checks if the value is unique. Change the @Length and @RandomString size to suit the size of the field that you need.