-3
 DECLARE @chars NCHAR(36)
SET @chars = N'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'

DECLARE @result NCHAR(4)
SET @result = SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1)
            + SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1)
            + SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1)
            + SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1)

SELECT @result

i try this but it's too long Im using nodejs and posgresql

tripleee
  • 175,061
  • 34
  • 275
  • 318
Bob sux
  • 11
  • 1
  • 6
  • What is a "discord id"? Show an example. State what the constraints are. It is unclear why you have strange string of alpha chars in your question. –  Jan 18 '17 at 02:40
  • like #8016 @jdv – Bob sux Jan 18 '17 at 02:55
  • You should put details in the original question, which can be edited by clicking the [edit](http://stackoverflow.com/posts/41710457/edit) button. –  Jan 18 '17 at 02:57
  • You just need a random number in a specific range? http://stackoverflow.com/q/2175512/1531971 –  Jan 18 '17 at 02:59
  • Or maybe you are just trying to create a unique identity in SQL? http://stackoverflow.com/q/29581460/1531971 –  Jan 18 '17 at 03:04
  • i want to generate unique id for my users without any range – Bob sux Jan 18 '17 at 03:04
  • You should state that in your question. This is a pretty basic question, though, and is likely answered more than a few times. In your case, I suspect "serial numeric type" might be what you need. –  Jan 18 '17 at 03:05
  • 2
    Please don't vandalize your post. It is no longer exclusively yours, as per the license you accepted when you posted here. – tripleee Jan 19 '17 at 08:06

2 Answers2

1

You can generate the id with your Node.js app. Do a random with Math

Math.random().toString(36).substr(2, 4) // You can change the `4` to change the length.

It will output a string like this: 92q6

In details:

Math.random() // 0.2433971674181521
Math.random().toString(36) // 0.ghlhdmdypp8bmx6r

The toString method of a number type in javascript takes an optional parameter to convert the number into a given base. If you pass two, for example, you'll see your number represented in binary. Similar to hex (base 16), base 36 uses letters to represent digits beyond 9. By converting a random number to base 36, you'll wind up with a bunch of seemingly random letters and numbers.

Chris Baker

Paul Rey
  • 1,270
  • 1
  • 15
  • 26
  • hi bro thx for answering how many variation it could produces does it has limit if so how many . – Bob sux Jan 19 '17 at 04:39
  • You're right, if you want to get a real unique random you must use another way. As mentionned in the Math.random() MDN page: "Math.random() does not provide cryptographically secure random numbers. Do not use them for anything related to security. Use the Web Crypto API instead, and more precisely the window.crypto.getRandomValues() method." - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random – Paul Rey Apr 04 '19 at 09:17
0

Since you are using nodejs, you could use a standard integer in the database, and convert it to base36 when showing it to the user. In javascript, the parameter to toString on a numer is the base:

a = 46656
b = a.toString(36) // b = "1000"

And if you want to convert it back from base36 to a number, use parseInt:

b = "1000"
c = parseInt(c, 36) // c = 46656

If you want the string to be 4 characters, generate a number between 46656 ("1000") and 1679615 ("zzzz").

some
  • 48,070
  • 14
  • 77
  • 93