ORACLE SQL:Get all integers between two numbers

前端 未结 13 1136
说谎
说谎 2020-12-03 13:42

Is there any way to select the numbers (integers) that are included between two numbers with SQL in Oracle; I don\'t want to create PL/SQL procedure or function.

Fo

相关标签:
13条回答
  • 2020-12-03 14:09

    Gary, to show the result that he explained, the model query will be:

    SELECT c1 FROM DUAL MODEL DIMENSION BY (1 as rn)
    MEASURES (1 as c1) RULES ITERATE (8) (c1[ITERATION_NUMBER]=ITERATION_NUMBER+3) ORDER BY rn

    ;)

    I always use:

    SELECT (LEVEL - 1) + 3 as result FROM Dual CONNECT BY Level <= 8

    Where 3 is the start number and 8 is the number of "iterations".

    0 讨论(0)
  • 2020-12-03 14:09

    I want to share an usefull query that converts a string of comma and '-' separated list of numbers into a the equivalent expanded list of numbers:

    An example that converts '1,2,3,50-60' into

    1
    2
    3
    50
    51
    ...
    60
    
    select distinct * from (SELECT (LEVEL - 1) + mini as result FROM (select REGEXP_SUBSTR (value, '[^-]+', 1, 1)mini ,nvl(REGEXP_SUBSTR (value, '[^-]+', 1, 2),0) maxi from (select REGEXP_SUBSTR (value, '[^,]+', 1, level) as value from (select '1,2,3,50-60' value from dual) connect by level <= length(regexp_replace(value,'[^,]*'))+1)) CONNECT BY Level <= (maxi-mini+1)) order by 1 asc;
    

    You may use it as a view and parametrize the '1,2,3,50-60' string

    0 讨论(0)
  • 2020-12-03 14:14
    create table numbers (value number);
    
    declare
        x number;
    begin
        for x in 7 .. 25
        loop
            insert into numbers values (x);
        end loop;
    end;
    /
    
    0 讨论(0)
  • 2020-12-03 14:16

    This trick with Oracle's DUAL table also works:

    SQL> select n from
      2  ( select rownum n from dual connect by level <= 10)
      3  where n >= 3;
    
             N
    ----------
             3
             4
             5
             6
             7
             8
             9
            10
    
    0 讨论(0)
  • 2020-12-03 14:16

    In addition to the answers already provided, it is possible to combine the listagg function with connect by to obtain the result in the format mentioned in the question. See a code example below:

    SELECT 
       DBMS_LOB.SUBSTR(LISTAGG(S.INTEGERS,',' ) WITHIN GROUP (ORDER BY S.INTEGERS), 300,1) RESULT 
    FROM 
       (SELECT 
          INTEGERS 
       FROM 
          ( SELECT ROWNUM INTEGERS FROM DUAL CONNECT BY LEVEL <= 10) 
       WHERE 
          INTEGERS >= 3 
       ) S;
    

    OutPut:

    SQL> 
    RESULT
    ----------------
    3,4,5,6,7,8,9,10
    
    0 讨论(0)
  • 2020-12-03 14:19

    I just did a table valued function to do this in SQL server, if anyone is interested, this works flawlessly.

    CREATE FUNCTION [dbo].[NumbersBetween]
    (
        @StartN int,
        @EndN int
    )
    RETURNS 
    @NumberList table
    (
        Number int
    )
    
    AS
    
    BEGIN
    
    WHILE @StartN <= @EndN
        BEGIN
        insert into @NumberList
        VALUES (@StartN)
        set @StartN = @StartN + 1
        END
    
    Return
    
    END
    GO
    

    If you run the query: "select * from dbo.NumbersBetween(1,5)" (w/o the quotes of course) the result will be

    Number
    -------
    1
    2
    3
    4
    5
    
    0 讨论(0)
提交回复
热议问题