Click here to Skip to main content
15,905,877 members
Home / Discussions / Database
   

Database

 
GeneralRe: Splitting Rows in SQL Server 2005 [modified] Pin
Niladri_Biswas30-Jun-09 20:45
Niladri_Biswas30-Jun-09 20:45 
AnswerRe: Splitting Rows in SQL Server 2005 [modified] Pin
rajanandal30-Jun-09 20:53
rajanandal30-Jun-09 20:53 
GeneralGreen Screen Studio Pin
atlas2130-Jun-09 17:07
atlas2130-Jun-09 17:07 
GeneralRe: Green Screen Studio Pin
Mycroft Holmes30-Jun-09 17:49
professionalMycroft Holmes30-Jun-09 17:49 
GeneralRe: Green Screen Studio Pin
Jerry Hammond1-Jul-09 3:44
Jerry Hammond1-Jul-09 3:44 
QuestionRandom Access to DB Pin
LucBite30-Jun-09 4:26
LucBite30-Jun-09 4:26 
AnswerRe: Random Access to DB Pin
David Mujica30-Jun-09 4:48
David Mujica30-Jun-09 4:48 
AnswerRe: Random Access to DB [modified] Pin
Niladri_Biswas30-Jun-09 4:53
Niladri_Biswas30-Jun-09 4:53 
Hi,

The random numbers can be generated in multiple ways.

But in all the cases the questions should have a unique QuestionId( a Primary Key)

Step 1:

Create a table (say tblQuestions) with QuestionId(int Primary Key), Questions(Varchar(50))

Step 2:

Insert some values:

QuestionId Questions
---------------------
1	Q1
2	Q2
3	Q3
4	Q4
5	Q5
6	Q6
7	Q7
8	Q8
9	Q9
10	Q10
11	Q11
12	Q12
13	Q20
14	Q25
15	Q30


Solution 1:

Suppose you have a fixed set of questions (say 1 to 30).

Fire this query

SELECT QUESTIONS 

FROM TBLQUESTIONS 

WHERE QUESTIONID = ROUND(((30 - 1) * RAND() + 1), 0)


where 30 is the upper limit and 1 is lower limit

Solution 2:

Suppose your question may grow to any length dynamically

Fire this query

SELECT QUESTIONS 

FROM TBLQUESTIONS 

WHERE QUESTIONID = CAST(RAND() * N AS INT)


Where N is any number.

Means if you have records(i.e. Questions) from 1 to 10, N = 10

if you have records(i.e. Questions) from 1 to 50, N = 50

if you have records(i.e. Questions) from 1 to 100, N = 100 etc.



Solution 3:

For the same above scenario, the following query can also be use

SELECT QUESTIONS 

FROM TBLQUESTIONS 

WHERE QUESTIONID = ABS(CAST(NEWID() AS binary(6)) %N) + 1


Where N is any number.

Solution 4:

Send the random numbers from your front end application to the backend either in INLINE SQL or in STORED PROC, and retieve the questions based on the QuestionId.


Hope this helps
Smile | :)

Niladri Biswas

modified on Tuesday, June 30, 2009 11:26 AM

QuestionSQL server 2003 Pin
jamith30-Jun-09 2:52
jamith30-Jun-09 2:52 
AnswerRe: SQL server 2003 Pin
Jerry Hammond30-Jun-09 13:25
Jerry Hammond30-Jun-09 13:25 
AnswerRe: SQL server 2003 Pin
Mycroft Holmes30-Jun-09 17:51
professionalMycroft Holmes30-Jun-09 17:51 
QuestionSQL 2005 Replication Pin
Abdul Rahman Hamidy30-Jun-09 2:29
Abdul Rahman Hamidy30-Jun-09 2:29 
Generaldatabase Performance...... Pin
Isaac Gordon29-Jun-09 23:59
Isaac Gordon29-Jun-09 23:59 
GeneralRe: database Performance...... Pin
Blue_Boy30-Jun-09 0:15
Blue_Boy30-Jun-09 0:15 
GeneralRe: database Performance...... Pin
Niladri_Biswas30-Jun-09 0:26
Niladri_Biswas30-Jun-09 0:26 
GeneralRe: database Performance...... Pin
Isaac Gordon30-Jun-09 0:33
Isaac Gordon30-Jun-09 0:33 
QuestionHidden tables Pin
mistryshailesh29-Jun-09 21:52
mistryshailesh29-Jun-09 21:52 
AnswerRe: Hidden tables Pin
Niladri_Biswas29-Jun-09 23:10
Niladri_Biswas29-Jun-09 23:10 
QuestionOLEDB network failure recovery problem. Pin
CherezZaboro29-Jun-09 6:57
CherezZaboro29-Jun-09 6:57 
QuestionEncryptbyKey VARCHAR(50) Pin
Saamir29-Jun-09 6:42
Saamir29-Jun-09 6:42 
QuestionMy Sql Query Help Pin
Hulicat29-Jun-09 6:26
Hulicat29-Jun-09 6:26 
AnswerRe: My Sql Query Help Pin
Niladri_Biswas29-Jun-09 6:54
Niladri_Biswas29-Jun-09 6:54 
GeneralRe: My Sql Query Help Pin
Hulicat29-Jun-09 11:23
Hulicat29-Jun-09 11:23 
GeneralRe: My Sql Query Help [modified] Pin
Niladri_Biswas29-Jun-09 19:33
Niladri_Biswas29-Jun-09 19:33 
GeneralRe: My Sql Query Help Pin
Hulicat1-Jul-09 7:06
Hulicat1-Jul-09 7:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.