Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Random alphanumeric generator

Random alphanumeric generator

Scheduled Pinned Locked Moved Database
databasecsharpsql-serveroraclesysadmin
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    totig
    wrote on last edited by
    #1

    Hi, I need to create a 6 character random alphnumberic serial number. How can this be achieved in SQL Server 2000? I have been searching and found that Oracle has a function for this, so I am assuming there should be one for SQL then. I could do this easily in .net - but I dont want to have to keep sending a request to the database to check that this number is unique. I would prefer to do the whole thing in a SQL function. So far the only way I have been able to work out, is too create a new GUID - select 6 characters from it - then check that these are not already used in the table (otherwise create a new one and repeat) and insert it. Any help on this? Thanks Wayde

    T 1 Reply Last reply
    0
    • T totig

      Hi, I need to create a 6 character random alphnumberic serial number. How can this be achieved in SQL Server 2000? I have been searching and found that Oracle has a function for this, so I am assuming there should be one for SQL then. I could do this easily in .net - but I dont want to have to keep sending a request to the database to check that this number is unique. I would prefer to do the whole thing in a SQL function. So far the only way I have been able to work out, is too create a new GUID - select 6 characters from it - then check that these are not already used in the table (otherwise create a new one and repeat) and insert it. Any help on this? Thanks Wayde

      T Offline
      T Offline
      totig
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups