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 > How to update a DB2 column with a random number?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-02-06, 11:25
nsanjeevk nsanjeevk is offline
Registered User
 
Join Date: Nov 2004
Posts: 7
How to update a DB2 column with a random number?

Hi,
Can someone please help me with this one.

I want to update a DB2 table column (Character field) with a random number. I need a SQL for this.

Thanks.
Reply With Quote
  #2 (permalink)  
Old 03-02-06, 12:57
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #3 (permalink)  
Old 03-02-06, 13:37
nsanjeevk nsanjeevk is offline
Registered User
 
Join Date: Nov 2004
Posts: 7
Thanks for the link. I checked it already. Actually I need one with UPDATE command.
Reply With Quote
  #4 (permalink)  
Old 03-02-06, 18:22
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
So, are you expecting someone to write you an UPDATE Statement ?

If your employer is paying you to write this query, I think it's fair that you give a share of the money to the person who writes the update statement for you ..

Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #5 (permalink)  
Old 03-02-06, 19:22
nsanjeevk nsanjeevk is offline
Registered User
 
Join Date: Nov 2004
Posts: 7
Hey, take it easy...I tried with all options and couldn't get thru. Then only I posted into this forum. My query was giving some problem. When I searched through internet, I saw this forum. I don't want anyone to do my job. Just asked for some help thinking that this forum is meant for that. Anyway thanks for your advise and help.
Reply With Quote
  #6 (permalink)  
Old 03-02-06, 19:46
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Post what you have tried and the error message you get ... You'll get better responses ....
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #7 (permalink)  
Old 03-03-06, 02:53
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
@ nsanjeevk

try this:

UPDATE your.table
SET column = CAST ( SMALLINT ( RAND () *10000 + 1 ) AS CHAR(5))
WHERE ...

this will set your column to a value between 1 and 10000

( DB2 for z/OS ).
May be you must adjust the query if your DB2 is running on an other platform.
Reply With Quote
  #8 (permalink)  
Old 03-03-06, 05:02
JAYANTA_DATTA JAYANTA_DATTA is offline
Registered User
 
Join Date: Oct 2004
Location: DELHI INDIA
Posts: 336
Hi,

On UDB we run the following to get random number..

db2 "select SMALLINT ( RAND () *10000 + 1 ) from sysibm.sysdummy1"

Based upon that you can write your UPDATE also.
Reply With Quote
  #9 (permalink)  
Old 03-03-06, 05:23
Rob den Heijer Rob den Heijer is offline
Registered User
 
Join Date: Feb 2006
Location: Utrecht, Netherlands
Posts: 16
Quote:
Originally Posted by nsanjeevk
Hey, take it easy...I tried with all options and couldn't get thru. Then only I posted into this forum. My query was giving some problem. When I searched through internet, I saw this forum. I don't want anyone to do my job. Just asked for some help thinking that this forum is meant for that. Anyway thanks for your advise and help.
I'm curious now... You tried with all options. What did you try?
Reply With Quote
  #10 (permalink)  
Old 03-03-06, 12:35
nsanjeevk nsanjeevk is offline
Registered User
 
Join Date: Nov 2004
Posts: 7
Hi, Appreciate all your help!!

Initially I tried with CAST(CHAR (RAND() * 1000000000 AS CHAR)) and also tried with different CAST and RIGHT functions. It gave me -433 and -413 SQL errors. As you mentioned, I just moved AS CHAR(9) outside and its working perfect. Here is the SQL which worked in my DB2 V 7.2...

SET Random_Num = CAST ( INT ( RAND () * 1000000000) AS CHAR(9))

Thanks again. Have a good one!!

Hi Sathyaram, please don't take me wrong. I just wanted to see different ideas from the forum. That's why I didn't put my SQL. Even I was not in hurry. If I give my SQL, will just get the correction to it. I won't get different ideas. Appreciate all your hardwork in this forum. Have a good one!!
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