:SetVar Function "AUX:RoundDiv.Ext"
:r $(DropFunctionScript_AUX)
create function [$(Function)]( @Divident int, @Divider int, @ZeroDivisionResNull bit=0 )
returns int
BEGIN
/*==================================================================================================
# SQLAUX scripting library | Author: Sergei Y.Kitáev
----------------------------------------------------------------------------------------------------
#
# ROUND DIVISION OF TWO INTEGERS (EXT.VERSION):
#
# === Usage: ===
# dbo.[AUX:RoundDiv.Ext]( <Divident>, <Divider>, 0 )
# dbo.[AUX:RoundDiv.Ext]( <Divident>, <Divider>, 1 )
# dbo.[AUX:RoundDiv.Ext]( <Divident>, <Divider>, null )
# dbo.[AUX:RoundDiv.Ext]( <Divident>, <Divider>, default )
#
==================================================================================================*/

if @ZeroDivisionResNull is null
   set @ZeroDivisionResNull = 0

if @Divident is null or @Divider is null or
   @Divider = 0 and @ZeroDivisionResNull = 1
   return null
/*
return ROUND( CONVERT(float,@Divident) / @Divider, 0, 0 )
*/
declare @Div int
set @Div = @Divident / @Divider
return
   case
      when ABS( @Divident % @Divider * 2 ) >= ABS( @Divider )
      then ( ABS( @Div ) + 1 ) * dbo.[AUX:IntSign.Int]( @Divident ) * dbo.[AUX:IntSign.Int]( @Divider )
      else @Div
   end

/*================================================================================================*/
END
GO
/*
select dbo.[AUX:RoundDiv.Ext]( 10, 3, null ) 'Round division'
select dbo.[AUX:RoundDiv.Ext]( 14, 9, default ) 'Round division'
*/
/*
select dbo.[AUX:RoundDiv.Ext]( 7, 0, 0 ) 'Round division'
select dbo.[AUX:RoundDiv.Ext]( 7, 0, 1 ) 'Round division'
select dbo.[AUX:RoundDiv.Ext]( 7, 0, null ) 'Round division'
select dbo.[AUX:RoundDiv.Ext]( 7, 0, default ) 'Round division'
*/
