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 String Generator

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-27-09, 12:18
db2udbgirl db2udbgirl is offline
Registered User
 
Join Date: Mar 2006
Location: Tuticorin, India
Posts: 100
Random String Generator

Env : DB2 9.1/AIX 5.3

Any method available in DB2 to generate a random string (as rand is for a random floating point).

Thanks in advance.
Reply With Quote
  #2 (permalink)  
Old 07-27-09, 12:52
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
There would be no such function or method in DB2 or another DBMS.

It will be better to define more specific. For example:
- Fixed length or variable length?
- Alphabet or alphanumeric or including special characters or any hex values?
- Code set(Unicode or ascii or another)?
etc.
Reply With Quote
  #3 (permalink)  
Old 07-27-09, 13:09
db2udbgirl db2udbgirl is offline
Registered User
 
Join Date: Mar 2006
Location: Tuticorin, India
Posts: 100
It would be fixed, alphanumeric and unicode

Eg: ddbe7f50-8348-4d9f-9c0f-38dcb3a46155

Datatype of this column is VARCHAR(64)
Reply With Quote
  #4 (permalink)  
Old 07-27-09, 13:34
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You can use the RAND() function, combined with HEX() and string concatenation.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #5 (permalink)  
Old 07-27-09, 17:31
db2udbgirl db2udbgirl is offline
Registered User
 
Join Date: Mar 2006
Location: Tuticorin, India
Posts: 100
That worked. Thanks
Reply With Quote
  #6 (permalink)  
Old 07-27-09, 18:27
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Someone thinking for themselves - that's really great and unfortunately rather rare these days! I was really expecting a response like "how do I do that?" :-)
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #7 (permalink)  
Old 07-28-09, 06:57
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Well done db2udbgirl ...

Can you post the code here please ?

Cheers
Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #8 (permalink)  
Old 07-28-09, 09:40
db2udbgirl db2udbgirl is offline
Registered User
 
Join Date: Mar 2006
Location: Tuticorin, India
Posts: 100
db2 -x "select substr(hex(rand(1)),1,8) ||'-'|| substr(hex(rand(2)),1,4)||'-'|| substr(hex(rand(3)),1,4)||'-'|| substr(hex(rand(4)),1,4)||'-'|| substr(hex(rand(5)),1,12) from sysibm.sysdummy1"

3FE071A0-3F9C-3FE1-3FAC-3FE237E46FC8
Reply With Quote
  #9 (permalink)  
Old 07-28-09, 11:35
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
You can use following Random String Generator, which is working on all platforms of DB2.
I just created it and tested many times:

Quote:
with input_tbl (g_string) as
( select
varchar('1234567890-=qwertyuiop[]\asdfghjkl;''zxcvbnm,./ !@#$%^&*()_+ASDFGHJKL:"ZXCVBNM<>?', 255)
from sysibm.sysdummy1
)
,
control_tbl (cntl_str, maxpos) as
(select g_string, length(g_string)
from input_tbl
)
,
rand_string_generator(k, reallength, random_string) as
(select 0, int(rand() * 100), varchar('', 100)
from sysibm.sysdummy1
union all
select k + 1, reallength, random_string || substr(cntl_str, int(rand() * maxpos + 1), 1)
from rand_string_generator, control_tbl
where k + 1 <= reallength
)
select random_string "Rand String"
from rand_string_generator
where k = reallength
Lenny K.
Reply With Quote
  #10 (permalink)  
Old 07-28-09, 14:46
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Quote:
Originally Posted by db2udbgirl
db2 -x "select substr(hex(rand(1)),1,8) ||'-'|| substr(hex(rand(2)),1,4)||'-'|| substr(hex(rand(3)),1,4)||'-'|| substr(hex(rand(4)),1,4)||'-'|| substr(hex(rand(5)),1,12) from sysibm.sysdummy1"

3FE071A0-3F9C-3FE1-3FAC-3FE237E46FC8
Is this a random ? You have to use:

Quote:
select substr(hex(rand()),1,8) ||'-'|| substr(hex(rand()),1,4)||'-'|| substr(hex(rand()),1,4)||'-'
|| substr(hex(rand()),1,4)||'-'|| substr(hex(rand()),1,12) from sysibm.sysdummy1
But even this one is not a real random string generator.

Sorry, Lenny

Last edited by Lenny77; 07-28-09 at 14:52.
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