Swag
Swag

Reputation: 2140

Add random string in a column to each row in database

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?

Upvotes: 2

Views: 3835

Answers (1)

DeanG
DeanG

Reputation: 647

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.

Upvotes: 3

Related Questions