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?

1
  • Can you show the table definition? Commented Nov 1, 2013 at 19:59

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

And if I set the ''Password'' entity to unique in the database?
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.
No I could change to more then 10 characters, but I don't want to have long passwords :)
With 1000 passwords or even 10000, the method above is (highly) unlikely to produce a duplicate, but as mentioned it is not guaranteed.
For discussion about unique identifiers, I recommend reading this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.