Question

I am trying to translate an MS SQL function to oracle I am running into trouble. They reason being creating temporary table within the function to gradually add to. I can't seem to replace the temporary table with a cursor to gradually add to. Someone has to have a good idea how to write this in Oracle:

ALTER FUNCTION [dbo].[F_GetDateIntervalTable]
(
@OccurredFrom datetime,
@OccurredTo datetime,
@Interval decimal
)
RETURNS @Tbl table
(
[Dts] datetime
)
AS
BEGIN
    DECLARE @Count int
    --DECLARE @Tbl table([Dts] datetime)
    DECLARE @Dts datetime
    DECLARE @SeedDts datetime

SET @Count = 1
SET @Dts = DATEADD(MINUTE, FLOOR(DATEDIFF(MINUTE,0,@OccurredFrom)/@Interval)*@Interval, 0);
SET @SeedDts = DATEADD(MINUTE, FLOOR(DATEDIFF(MINUTE,0,@OccurredFrom)/@Interval)*@Interval, 0);
SET @OccurredTo = DATEADD(MINUTE, -@Interval, @OccurredTo);
WHILE (@SeedDts < @OccurredTo)
BEGIN
    SET @SeedDts = DATEADD(MINUTE, @Interval*(@Count-1), @Dts)
    INSERT INTO @Tbl(Dts) VALUES(@SeedDts)
    SET @Count = (@Count + 1)
END

RETURN

END

The output should be this (given the parameters):

@OccurredFrom = '2013-01-01',
@OccurredTo = '2013-01-02',
@Interval = 60



2013-01-01 00:00:00.000
2013-01-01 01:00:00.000
2013-01-01 02:00:00.000
2013-01-01 03:00:00.000
2013-01-01 04:00:00.000
2013-01-01 05:00:00.000
2013-01-01 06:00:00.000
2013-01-01 07:00:00.000
2013-01-01 08:00:00.000
2013-01-01 09:00:00.000
2013-01-01 10:00:00.000
2013-01-01 11:00:00.000
2013-01-01 12:00:00.000
2013-01-01 13:00:00.000
2013-01-01 14:00:00.000
2013-01-01 15:00:00.000
2013-01-01 16:00:00.000
2013-01-01 17:00:00.000
2013-01-01 18:00:00.000
2013-01-01 19:00:00.000
2013-01-01 20:00:00.000
2013-01-01 21:00:00.000
2013-01-01 22:00:00.000
2013-01-01 23:00:00.000

Any ideas are grealty appreciated!

Was it helpful?

Solution

Assuming you need to use a function rather than simply writing a SQL query

SQL> ed
Wrote file afiedt.buf

  1  with x as (
  2    select date '2013-01-01' start_date,
  3           date '2013-01-02' end_date,
  4           60 interval
  5      from dual
  6  )
  7  select start_date + numtodsinterval( interval * (level-1), 'minute' )
  8    from x
  9* connect by level <= (end_date - start_date)*24*60/interval
SQL> /

START_DATE+NUMTODSI
-------------------
2013-01-01 00:00:00
2013-01-01 01:00:00
2013-01-01 02:00:00
2013-01-01 03:00:00
2013-01-01 04:00:00
2013-01-01 05:00:00
2013-01-01 06:00:00
2013-01-01 07:00:00
2013-01-01 08:00:00
2013-01-01 09:00:00
2013-01-01 10:00:00
2013-01-01 11:00:00
2013-01-01 12:00:00
2013-01-01 13:00:00
2013-01-01 14:00:00
2013-01-01 15:00:00
2013-01-01 16:00:00
2013-01-01 17:00:00
2013-01-01 18:00:00
2013-01-01 19:00:00
2013-01-01 20:00:00
2013-01-01 21:00:00
2013-01-01 22:00:00
2013-01-01 23:00:00

24 rows selected.

you can create a pipelined table function

SQL> create type tbl_date as table of date;
  2  /

Type created.

SQL> create or replace function get_date_interval( p_start_date in date,
  2                                                p_end_date   in date,
  3                                                p_interval   in number )
  4    return tbl_date
  5    pipelined
  6  is
  7    l_return_dt date := p_start_date;
  8  begin
  9    while( l_return_dt < p_end_date )
 10    loop
 11      pipe row( l_return_dt );
 12      l_return_dt := l_return_dt + numtodsinterval( p_interval, 'minute' );
 13    end loop;
 14    return;
 15  end;
 16  /

Function created.

SQL> select *
  2    from table( get_date_interval( date '2013-01-01',
  3                                   date '2013-01-02',
  4                                   60 ));

COLUMN_VALUE
-------------------
2013-01-01 00:00:00
2013-01-01 01:00:00
2013-01-01 02:00:00
2013-01-01 03:00:00
2013-01-01 04:00:00
2013-01-01 05:00:00
2013-01-01 06:00:00
2013-01-01 07:00:00
2013-01-01 08:00:00
2013-01-01 09:00:00
2013-01-01 10:00:00
2013-01-01 11:00:00
2013-01-01 12:00:00
2013-01-01 13:00:00
2013-01-01 14:00:00
2013-01-01 15:00:00
2013-01-01 16:00:00
2013-01-01 17:00:00
2013-01-01 18:00:00
2013-01-01 19:00:00
2013-01-01 20:00:00
2013-01-01 21:00:00
2013-01-01 22:00:00
2013-01-01 23:00:00

24 rows selected.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top