Dates and Random Numbers in PL/SQL?

Dates and Random Numbers in PL/SQL?

Post by Jason C Mille » Wed, 04 Jun 1997 04:00:00



Hi,

 I'm starting my first sizable effort in PL/SQL programming and I'm
stuck against two small walls.  (My configuration is Oracle 7.3 and
Oracle Webserver Cartridges).

 One thing I want to do is easily work with dates.  I've seen functions
to change the date, but not times (except for the time zone stuff).  I'd
like to write a function that does the following:
  new_date := old_date + hours + minutes;
Ie. I'd like to take a date and add an hour and/or minutes to it.  I
realize I could do this by converting it to a character string and doing
all kinds of manipulations with it-- is there an easier way?

  Secondly, I'd like to generate _large_ highly random numbers.  The
random.rndint() utility with OWS2.1 generates randoms, but it seems to
have problems generating any number beyond 9999.  I'd like to generate a
number between 0 and 10^9 if possible.  I am currently planning to do
this by generating a number of them and concat'ing them together.  Are
there any public domain routines for generating integers?

  Lastly, a general question: is anyone doing anything inventive for
testing PL/SQL statements?  Currently I've been doing stuff like:

SQL> declare
      foobar number;
     begin
      foobar := mypackage.myfunct(242);
      insert into temp values(tochar(foobar));
     end;

and such.  I cannot believe there isn't a nice utility I should be using
to get the return values printed out to my display?

I'd appreciate any help that you can offer.  Many thanks for reading.

Regards,
Jason

-------------------------------------------------------------------------------


|\\|   Carnegie Mellon University    |//|                     FAX  412/361-8399
   In the end we will conserve only what we love, we will love only what we
          understand, we will understand only what we are taught.

 
 
 

Dates and Random Numbers in PL/SQL?

Post by Jurij Mod » Wed, 04 Jun 1997 04:00:00


On Tue,  3 Jun 1997 02:02:09 -0400, Jason C Miller


>Hi,

> I'm starting my first sizable effort in PL/SQL programming and I'm
>stuck against two small walls.  (My configuration is Oracle 7.3 and
>Oracle Webserver Cartridges).

> One thing I want to do is easily work with dates.  I've seen functions
>to change the date, but not times (except for the time zone stuff).  I'd
>like to write a function that does the following:
>  new_date := old_date + hours + minutes;
>Ie. I'd like to take a date and add an hour and/or minutes to it.  I
>realize I could do this by converting it to a character string and doing
>all kinds of manipulations with it-- is there an easier way?

Date type in oracle includes also time portion and all date
arithmetics in oracle take time portion in account. With
adding/subtracting dates the main unit is one day, but you can easily
add/subtract hours or minutes by adding/subtatcing corresponding
decimal portion of an unit. For example:
        new_date := old_date + 1/24 + 10/60
or
        new_date := old_date + (60+10)/(24*60)
will both add 70 minutes to new_date.

Quote:

>  Secondly, I'd like to generate _large_ highly random numbers.  The
>random.rndint() utility with OWS2.1 generates randoms, but it seems to
>have problems generating any number beyond 9999.  I'd like to generate a
>number between 0 and 10^9 if possible.  I am currently planning to do
>this by generating a number of them and concat'ing them together.  Are
>there any public domain routines for generating integers?

You can get first random integer in the range of 0-9999 and multiply
it by 10^5. Then get next random in the same range and multiply it by
10. Get one more random integer in the range 0-9. Finaly add all this
three numbers together. That way you will get pretty equal
distribution of randoms between 0 and 10^9.

Quote:>  Lastly, a general question: is anyone doing anything inventive for
>testing PL/SQL statements?  Currently I've been doing stuff like:

>SQL> declare
>      foobar number;
>     begin
>      foobar := mypackage.myfunct(242);
>      insert into temp values(tochar(foobar));
>     end;

>and such.  I cannot believe there isn't a nice utility I should be using
>to get the return values printed out to my display?

Most commonly used technique for server side PL/SQL debugging is using
supplyed package called DBMS_OUTPUT witch can be used to display
strings from PL/SQL blocks to your display within your SQL*Plus
sessin. In your example you would write:

SQL> SET SERVEROUTPUT ON
SQL> declare
      foobar number;
     begin
      foobar := mypackage.myfunct(242);
--      insert into temp values(tochar(foobar));
      DBMS_OUTPUT.PUT_LINE(TO_CHAR(foobar));
     end;

After your PL/SQL block is executed, the value of foobar will be
printed on your SQL*Plus window.

There are some other techniques with other oracle packages witch can
be used, but are even more complicated and clumsy to use for debugging
purposes.

After Oracle8 is out third party vendors will provide true PL/SQL
de*s, but until then you will have to stick with DBMS_OUTPUT.

Quote:

>I'd appreciate any help that you can offer.  Many thanks for reading.

>Regards,
>Jason

============================================================
Jurij Modic                             Republic of Slovenia
tel: +386 61 178 55 14                  Ministry of Finance
fax: +386 61  21 45 84                  Zupanciceva 3

============================================================

 
 
 

Dates and Random Numbers in PL/SQL?

Post by Timo Haataine » Thu, 05 Jun 1997 04:00:00



> Hi,

>  I'm starting my first sizable effort in PL/SQL programming and I'm
> stuck against two small walls.  (My configuration is Oracle 7.3 and
> Oracle Webserver Cartridges).

>  One thing I want to do is easily work with dates.  I've seen functions
> to change the date, but not times (except for the time zone stuff).  I'd
> like to write a function that does the following:
>   new_date := old_date + hours + minutes;
> Ie. I'd like to take a date and add an hour and/or minutes to it.  I
> realize I could do this by converting it to a character string and doing
> all kinds of manipulations with it-- is there an easier way?

> <snip>

Hi Jason,
here is something to your date arithmetics question:

Add 1 day  to system date:  sysdate + 1
Add 1 hour to system date:  sysdate + 1/24
Add 1 minute ---- " -----:  sysdate + 1/(24*60)

Unfortunately this doesn't handle DST changes. Suppose summertime begins (Finnish  
timezone) 30-Mar-1997 03:00 when clock is turned to be 30-Mar-1997 04:00. The
following query gives wrong result (there is no 03:30 in Finnish timezone in given
day):
SQL> alter session set nls_date_format='YYYY.MM.DD HH24:MI:SS';
SQL> select to_date('1997.03.30 02:30:00')+1/24 from dual;

TO_DATE('1997.03.30
-------------------
1997.03.30 03:30:00

Usually this is not a big issue and could be solved by storing critical date-columns
in UTC-time.

--

Timo Haatainen
Carelcomp Forest Oy

 
 
 

1. Random numbers from PL/SQL to Java

Hi,
I should have a random number from my ID products to show
them 'randomly' in a WEB.
I wrote this Stored procedure in Oracle 8i :
PROCEDURE product_random(generic_cv OUT GenCurTyp) IS

CURSOR CUR1 IS SELECT Products.ID FROM Products,Companies WHERE
Products.status=1
AND Products.IDCompany = Companies.ID
AND Companies.online1 = 1 AND Companies.approved = 1 AND
Companies.catal_appr = 1;
c1 NUMBER;
ID1 NUMBER;
ID_OUT NUMBER;
tmp number;

BEGIN

SELECT COUNT(Products.ID) INTO c1 FROM Products, Companies WHERE
Products.status=1 AND Products.IDCompany = Companies.ID AND
Companies.online1 = 1 AND Companies.approved = 1 AND
Companies.catal_appr = 1;

IF c1>0 THEN BEGIN
tmp := RAND.RANDOM()*(c1)+1;
dbms_output.put_line('tmp '| |tmp);
ID1:=TRUNC(tmp);
dbms_output.put_line('ID1 '| |ID1);
FOR E1 IN CUR1 LOOP
ID_OUT:=E1.ID;
ID1:=ID1-1;
IF ID1=0 THEN EXIT; END IF;
END LOOP;
END;
ELSE ID_OUT:=0;
END IF;

OPEN generic_cv FOR SELECT to_char(ID_OUT) FROM DUAL;
END;

When I call it from PLSQL works very fine but if I call it from
Java always an error apperars :
java.sql.SQLException: ORA-06502: PL/SQL: errore : ORA-06512:
conversion error a "MEGLO_DBA.RAND", line 14 ORA-06512:
a "MEGLO_DBA.GET", line 1736 ORA-06512: a line 1

Some ideas ??
Thanks a lot for all help

2. 'RETURN'-key default in Hyperscript Dialog Box

3. Random Numbers in PL/SQL

4. New Positions In London

5. PL/SQL Generating a random number

6. Edit region "behind" a screen picture?

7. Random numbers in PL/SQL.

8. Dimension Hierarchy Question

9. Random Number Generation in PL/SQL

10. Random numbers (PL/SQL Package attached)

11. PL-SQL Random Number Generator?

12. Looking for RANDOM number generator technique in PL/SQL