How to generate a range of numbers between two numbers?

后端 未结 30 2272
执念已碎
执念已碎 2020-11-22 10:16

I have two numbers as input from the user, like for example 1000 and 1050.

How do I generate the numbers between these two numbers, using

30条回答
  •  没有蜡笔的小新
    2020-11-22 10:20

    ;WITH u AS (
        SELECT Unit FROM (VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) v(Unit)
    ),
    d AS (
        SELECT 
            (Thousands+Hundreds+Tens+Units) V
        FROM 
               (SELECT Thousands = Unit * 1000 FROM u) Thousands 
               ,(SELECT Hundreds = Unit * 100 FROM u) Hundreds 
               ,(SELECT Tens = Unit * 10 FROM u) Tens 
               ,(SELECT Units = Unit FROM u) Units
        WHERE
               (Thousands+Hundreds+Tens+Units) <= 10000
    )
    
    SELECT * FROM d ORDER BY v
    

提交回复
热议问题