SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 Database Design and Application Architecture
 Populate column with 3 random strings from a list
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

bernard75
Starting Member

3 Posts

Posted - 07/12/2012 :  12:01:11  Show Profile  Reply with Quote
Long story short, i would like to populate a column with 3 random strings from a list. I have this:
UPDATE table SET column = ELT(FLOOR(RAND()*3)+1, 'string1', 'string2', 'string3');

Just need something to add 2 more strings. So the output will be:
string1 string2 string3
string3 string1 string2
string2 string3 string1
...

I hope thats no utter gibberish and would appreciate any help.
Cheers
Bernard

www.worldcupfancamp.com

Edited by - bernard75 on 07/12/2012 12:07:34

Lamprey
Flowing Fount of Yak Knowledge

3833 Posts

Posted - 07/12/2012 :  12:25:41  Show Profile  Reply with Quote
ELT isn't a SQL server function. Is that MySql? If you are using SQL Server 2012 you can make use of the CHOOSE function. But, I'm not 100% what your actual question is. Are you asking how to populate three columns from a set of three distinct values such that each column (in that row) gets a unique value?
Go to Top of Page

bernard75
Starting Member

3 Posts

Posted - 07/12/2012 :  12:36:02  Show Profile  Reply with Quote
Ooops, i thought it is:
http://www.tutorialspoint.com/sql/sql-string-functions.htm

What i am trying to accomplish, is to populate 1 column with 3 random values:
string1 string2 string3
string3 string1 string2
string2 string3 string1

www.worldcupfancamp.com
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

3833 Posts

Posted - 07/12/2012 :  14:00:34  Show Profile  Reply with Quote
For some reason I'm getting NULL values with the CHOOSE function, but here is one way
CREATE TABLE Foo (Col1 VARCHAR(20), Col2 VARCHAR(20), Col3 VARCHAR(20))
GO

INSERT Foo 
VALUES(
CHOOSE((ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) % 3) + 1, 'string1', 'string2', 'string3'),
CHOOSE((ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) % 3) + 1, 'string1', 'string2', 'string3'),
CHOOSE((ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) % 3) + 1, 'string1', 'string2', 'string3')
)
GO 10

SELECT *
FROM Foo

DROP TABLE Foo
Also, you could select from a table:
CREATE TABLE Val (Col VARCHAR(20));
INSERT Val VALUES ('string1'), ('string2'), ('string3');

CREATE TABLE Foo (Col1 VARCHAR(20), Col2 VARCHAR(20), Col3 VARCHAR(20));
GO

INSERT Foo 
VALUES(
	(SELECT TOP 1 Col FROM Val ORDER BY NEWID()),
	(SELECT TOP 1 Col FROM Val ORDER BY NEWID()),
	(SELECT TOP 1 Col FROM Val ORDER BY NEWID())
)
GO 10

SELECT *
FROM Foo

DROP TABLE Foo
DROP TABLE Val
Go to Top of Page

bernard75
Starting Member

3 Posts

Posted - 07/13/2012 :  03:04:24  Show Profile  Reply with Quote
Not what i was looking for, but it helps, thx.

Edited by - bernard75 on 07/13/2012 03:30:49
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000