SQL Functions - factorial

前端 未结 12 1495
傲寒
傲寒 2020-11-30 14:55

I am a beginner in SQL Functions. What is the best way to create a function for factorial in SQL Server- Say 10!

12条回答
  •  被撕碎了的回忆
    2020-11-30 15:26

    If you are okay with an approximation, use Stirling's Approximation.

    create table #temp (value int)
    
    insert into #temp values (5),(6),(7),(8)
    
    select 
        value,
        sqrt(2*3.14*value)*power((value/2.718),value) --stirling's approx.
    from #temp
    

    Note that you will have to make a case for 0!, if needed.

提交回复
热议问题