If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Random Date Generation

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-01-04, 11:02
vivekjdesai vivekjdesai is offline
Registered User
 
Join Date: Sep 2004
Posts: 1
Random Date Generation

I need to extract some test data from my database into an excel sheet. Test data records should have certain columns from employee database table plus randomly-generated birth dates.

So a sample test data sheet would be have first_name, last_name, dob_yyyy, dob_mm, dob_dd columns.

Here I would like to fetch first_name and last_name from employees table but dob columns have to be generated randomly.

Can anyone give me hints on how to go about random date generation.

thanks,
Vivek
Reply With Quote
  #2 (permalink)  
Old 09-01-04, 13:22
n_i n_i is online now
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
how about
Code:
date(int(rand() * 3652059))
Reply With Quote
  #3 (permalink)  
Old 09-01-04, 18:47
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
If you want your employees to be born between, say 1955 and 1985, then

values(date('1955-01-01') + INT(RAND()*30) YEARS + INT(RAND()*11) MONTHS + INT(RAND()*27) DAYS )

HTH

Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #4 (permalink)  
Old 09-02-04, 12:49
macjoubert macjoubert is offline
Registered User
 
Join Date: Oct 2003
Location: Rhodesia
Posts: 28
In the same fashion is there a way to generate random text only of n characters?
Reply With Quote
  #5 (permalink)  
Old 09-02-04, 12:56
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
can you give an example, please ????
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #6 (permalink)  
Old 09-02-04, 14:16
macjoubert macjoubert is offline
Registered User
 
Join Date: Oct 2003
Location: Rhodesia
Posts: 28
I mean is there some way you can generate random alphabets

like char (rand () ABC )) ; => is there something like this?

which should return the following values for 3(n) iterations.

abc,bca,cba,.... and so on. Or something similar
Reply With Quote
  #7 (permalink)  
Old 09-03-04, 06:31
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Quote:
Originally Posted by macjoubert
I mean is there some way you can generate random alphabets
There's always a way! Don't know if this is the easiest way but it's the best I can do for now...

Code:
CREATE FUNCTION SHUFFLE (P_START_STRING VARCHAR(2000))
RETURNS VARCHAR(2000)
LANGUAGE SQL
RETURN
WITH SHUFFLE (START_STRING, SHUFFLE_STRING, NEXT_CHAR_POS, LVL) AS
(
 VALUES (
           P_START_STRING
         , CAST('' AS VARCHAR(2000))
         , INT(RAND()*(LENGTH(P_START_STRING)))+1
         , LENGTH(P_START_STRING)
        )
 UNION ALL
 SELECT INSERT(START_STRING,NEXT_CHAR_POS,1,'')
 ,      SHUFFLE_STRING||SUBSTR(START_STRING,NEXT_CHAR_POS,1)
 ,      INT(RAND()*(LENGTH(RTRIM(START_STRING))-1))+1
 ,      LVL - 1
 FROM SHUFFLE
 WHERE LVL > 0
)
SELECT SHUFFLE_STRING FROM SHUFFLE WHERE LVL = 0
;
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On