2

I am using SQL Server 2008 and I got a database with around 1000 rows.

There is also a column called Password, which is empty for each row.

Normally I could do something like INSERT INTO Password VALUES("helloworld"); to add something into the Password column, but then all rows will get the same Password.

But I want to have a different Password for every single row in the database, a random word with 10 characters (strings and integers).

Can someone guide me to the right path how I can do this?

Swag
  • 2,090
  • 9
  • 33
  • 63

1 Answers1

3

Using a substring of a guid may work :

SELECT SUBSTRING(CONVERT(varchar(40), newid()), 1, 10)

If the dash is a problem, replace it.

SELECT SUBSTRING(REPLACE(CONVERT(varchar(40), newid()), '-', ''), 1, 10)

However, note that this does not guarantee a unique password for every row, since we are only using a part of a guid.

DeanG
  • 617
  • 4
  • 6
  • And if I set the ''Password'' entity to unique in the database? – Swag Nov 01 '13 at 20:09
  • Is the password limited to 10 characters ? Or can we get 32 characters ? If we can use 32 characters, then replacing the dashes will give you a unique value. With dashes, you will need 36 characters. – DeanG Nov 01 '13 at 20:20
  • No I could change to more then 10 characters, but I don't want to have long passwords :) – Swag Nov 01 '13 at 20:24
  • 1
    With 1000 passwords or even 10000, the method above is (highly) unlikely to produce a duplicate, but as mentioned it is not guaranteed. – DeanG Nov 01 '13 at 20:27
  • For discussion about unique identifiers, I recommend reading [this](https://stackoverflow.com/questions/39771/is-a-guid-unique-100-of-the-time) question. – MaLiN2223 Sep 27 '18 at 13:06