Using a SP to Round in 1,3 and 10 minute increments of an hour

  • I am using the the SP below to round in 1 ,3 and 10 minute increments of an hour.  It's rudimentary and doesn't always round properly.  I need it rounded to two decimal places.  Does anyone have any ideas how I could make it work better.  Be gentle this is my first try at writing a SP.

     

       declare @actualreference int

       declare @client char (12)

       declare @matter char (12)

       declare @timekeeper char (15)

       declare @operator char (15)

       declare @hours float

       declare @workdate datetime

       declare @rounding char (1)

       declare @rounding1 char(1)

       declare @newhours float

       declare @onemin float

       declare @inc float

       declare @name char (60)

       declare @minutes int

       declare @isexpense char (1)

      

    /* check for twentieth hours */

       if (@rounding1 = 'W') and (@isexpense = 'N') begin

         while (@minutes >= 3) begin

           select @minutes = @minutes - 3

         end

         if @minutes <> 0 begin

          select @inc = (3.0/60.0)

          select @newhours = @hours+(@onemin * ((@inc - (@minutes/60.0))*60.0))

         end

       end

    /* check for sixth hours */

       if (@rounding1 = 'S') and (@isexpense = 'N') begin

         while (@minutes >= 10) begin

           select @minutes = @minutes - 10

         end

         if @minutes <> 0 begin

          select @inc = (10.0/60.0)

          select @newhours = @hours+(@onemin * ((@inc - (@minutes/60.0))*60.0))

         end

       end

    /* check for sixtieth hours */

      if (@rounding1 = 'O') and (@isexpense = 'N') begin

        select @newhours = (ceiling(@hours*60.0)) / 60.0

        if round(@newhours,4) <> round(@hours,4) begin

        update [TBL1] set hours=@newhours where reference=@reference

        end

       end

     

     

  • The best programmers use almost no variables.  I think it can be simplified down to something like this:

    declare

     @minutes int,

     @incr int

    set @minutes = 29

    set @incr = 10 --1,3,10

    select round(((@minutes / @incr) * @incr) / 60.0, 2)

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply