Search code examples
oracle-databasenumbersrange

How to break number range to individual numbers oracle


How to break number range to individual numbers oracle

I have following number ranges in my table

START_SN END_SN
850000 850003
850015 850017

And need following result.

SN
850000
850001
850002
850003
850015
850016
850017

Solution

  • Sample table:

    SQL> SELECT * FROM number_ranges;
    
      START_SN     END_SN
    ---------- ----------
        850000     850003
        850015     850017
    

    This is one option which returns desired result (and avoids duplicates):

    SQL>   SELECT start_sn + COLUMN_VALUE - 1 AS sn
      2      FROM number_ranges
      3           CROSS JOIN
      4           TABLE (
      5              CAST (
      6                 MULTISET (    SELECT LEVEL
      7                                 FROM DUAL
      8                           CONNECT BY LEVEL <= end_sn - start_sn + 1) AS SYS.odcinumberlist))
      9  ORDER BY sn;
    
            SN
    ----------
        850000
        850001
        850002
        850003
        850015
        850016
        850017
    
    7 rows selected.
    
    SQL>