IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_RoundBanker]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN 
DECLARE @SQL VARCHAR(4000) = '
create function [dbo].[fn_RoundBanker]
( @NumToRound money, 
  @Digits Int
) RETURNS money  

as

BEGIN

  set @NumToRound = @NumToRound * power( 10, @Digits ) 

  return case 

           when @NumToRound = floor( @NumToRound ) 
           then @NumToRound 
           
           else case sign( ceiling( @NumToRound ) - 2 * @NumToRound + floor( @NumToRound ) ) 
                  
                  when 1 
                  then floor( @NumToRound ) 
                  
                  when -1 
                  then ceiling( @NumToRound ) 
                  
                  else 2 * round( @NumToRound / 2, 0 ) 
                  
                end 

         end / power( 10, @Digits )  

END'

EXEC(@SQL)
END


