Dr.Prog
Dr.Prog

Reputation: 243

How can I execute a custom round in SQL by 0.25 increments?

I'm trying to create a custom round in SQL that rounds up or down by .25 but I'm not certain if this is actually possible with what I'm trying to accomplish.
I've tried the ROUND statement but it seems you can only specify the number of decimal places you round the number to...I could be wrong here but looking for some guidance.

The rounding should follow the rules below:

First check:

So all values will either be:
X.00
X.25
X.50
X.75

For example,
4.416 should round up to 4.50
2.75 remains 2.75
5.834 should round down to 5.75
7.083 should round down to 7.00

Upvotes: 2

Views: 591

Answers (1)

John Cappelletti
John Cappelletti

Reputation: 81930

With just a little math... The convert(decimal(10,2),...) is optional

Declare @YourTable Table ([SomeCol] decimal(10,4))  Insert Into @YourTable Values 
 (4.416)
,(2.75)
,(5.834)
,(7.083)
 
Select *
      ,NewValue = convert(decimal(10,2),round(SomeCol/.25,0)*.25)
 From @YourTable

Results

SomeCol NewValue
4.4160  4.50
2.7500  2.75
5.8340  5.75
7.0830  7.00

Upvotes: 5

Related Questions