# sql function to round to specified increment

here is some sample data from a table I have named RoundInfo (ms sql server 2008)

Floor         Ceiling           percent    DollarIncrement
100                 200                 5                 1
201                 4001               7                 5

I am trying to write a sql function that will take in a parameter that will be
an integer amount

The function will do the following:

1. determine the range the parameter value falls into  (the ranges are set up so the input will definitely fall into one of them)

2  for the range determined above, add the floor and ceiling together and divide that sum by 2

3. multiply the above result by the percent

4. take the result from the result above and round based on the increment

For example,

if the parameter value passed in was 120

since 120 falls in the first range,  the function would do  (100 + 200)/2 = 150
then 150*.05 = 147.07

then since the round is set to nearest 1  increment  the  function should return a value of 8
(if the value were 7.49 the value returned should be 7)

Likewise,

if the parameter value passed in was 400

since 400 falls in the second range,  the function would do  (201 + 4001)/2 = 2101

then 2101*.07 = 147.07
then since the round is set to nearest 5 increment function should return a value of 145 (since 147.07 is closer to 145 than 150)

(if the value were 158 then the value returned should be 160)

Hopefully this makes sense
Asked:
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Senior DBACommented:
I've used CROSS APPLYs to break the computations out so they can be named to increase understanding of the code, making it easier to verify, and to simply the final calculations:

SELECT
*,
Minimum_Amount + CASE WHEN (Base_Amount - Minimum_Amount) >= DollarIncrement / 2.0
THEN DollarIncrement ELSE 0 END AS Rounded_Amount
FROM (
SELECT 100 AS Floor, 200 AS Ceiling, 5 AS [Percent], 1 AS DollarIncrement UNION ALL
SELECT 201, 4001, 7, 5
) AS test_data
CROSS APPLY (
SELECT (Floor + Ceiling) / 2.0 * [percent] / 100.0 AS Base_Amount
) AS assign_alias_names
CROSS APPLY (
SELECT FLOOR(Base_Amount / DollarIncrement) * DollarIncrement AS Minimum_Amount
) AS assign_alias_names2
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Excellent response as usual....Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Already a member? Login.

All Courses

From novice to tech pro — start learning today.