'Create a function with two parameters and list numbers between them

I need to list all numbers between user input's. Actually I need to list only the odd numbers but I can do that if I can list all numbers.

Here's what I have done so far

ALTER Function [dbo].[ofdnumbers] 
(
    @MaxValue as int,
    @MinValue as int
) 
Returns @table table( Sonuclar int not null)
as
Begin          
    Declare @Num as int
    set @Num=@MinValue
    set @Num=@Num+1

    insert into @table
        select sonuclar from @table
     
    return @table
end
 


Solution 1:[1]

One possible function you could implement would be something along the lines of the following.

As illustrated by the various linked articles there are numerous ways to create a number series, the best approach is probably to have a permanent numbers/tally table with enough rows to cater for all requirements that you can just directly select from.

The following (or something similar) will provide the best relative performance:

create or alter function dbo.OddNumbers(@MaxValue int, @MinValue int)
returns table
as
return 
select Sonuclar from (
  select top (@MaxValue - @MinValue + 1) 
    Row_Number() over(order by (select null)) + @MinValue - 1 as Sonuclar
  from master.dbo.spt_values
)n
where Sonuclar % 2 = 1;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stu