0

I have a table containing IDs and they are all in 3-letter words. How do I use SELECT statement that returns a randomly generated 3 letter ID that does not yet exist in the current table and ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 are the only characters that can be used?

I would like to provide something I have in mind but I totally have no idea how to do this. Thank you so much.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
chris_techno25
  • 2,401
  • 5
  • 20
  • 32
  • You could try using `HASHBYTES` - though you will get collisions - but you will with any RNG. – Dai Jul 13 '23 at 03:43
  • you will need to store all generated IDs (obviously) in a permanent table and check for existence. https://stackoverflow.com/questions/1324063/generating-random-strings-with-t-sql – Mitch Wheat Jul 13 '23 at 04:01
  • @MitchWheat Yes Sir, there is a table that contains the IDs. How do I Select generateID() where generateID() is not in..... Something like that? – chris_techno25 Jul 13 '23 at 04:13
  • check the link I posted .... – Mitch Wheat Jul 13 '23 at 04:15
  • 2
    @chris_techno25 you don't need to call anyone sir here... we're all equal... – Dale K Jul 13 '23 at 04:27
  • 3
    @DaleK But some of us are more equal than others... _especially_ if we're talking about JavaScript, what with `==` vs `===` yaknow? – Dai Jul 13 '23 at 04:30
  • @DaleK I'm certainly not an equal to most people here lol. I have limited knowledge. – chris_techno25 Jul 13 '23 at 04:40
  • 1
    @chris_techno25 no 2 people here have the same level of knowledge, but as people we are equals and no deference is necessary, nor is it welcomed by most of us. – Dale K Jul 13 '23 at 04:43
  • What if Sir Patrick Stewart or Sir Tim Berners-Lee posted on StackOverflow though? – Dai Jul 13 '23 at 06:22
  • Then you ask them how they wish to be addressed I guess – Dale K Jul 13 '23 at 06:38
  • There are two ways, either 1) write a loop that generates a random value until it doesn't exist or 2) generate all values once and then pick the one that isn't exist already. The first idea is bad because it might take a while to get a match, the second should be good if you only have 3 characters – siggemannen Jul 13 '23 at 08:24

0 Answers0