Hello good afternoon to everyone.
I'm having a problem when trying to round grades using SQL SERVER.
I attach some examples:
I need to round 8.6 to 8.5 (from 8.9 to 8.5 it stays at 8.5)
Try
SELECT ROUND(8.6, 1, 1) AS resultado;
Result (Not rounding me)
8.6
Another example is rounding from 8.1 to 8.0 (from 8.4 to 8.0 it stays at 8.0)
Try
SELECT ROUND(8.1, 1, 1) AS resultado;
Result (Not rounding me)
8.1
I hope you can support me.
Greetings and have a great Wednesday.
I don't think there is a function that does EXACTLY what you ask for. At least not a "built in Function". But...
If you want to get creative, you can do something like this:
parsename(Calificacion,1)
returns the right part of the number, that is, the decimal part. This is what we want to evaluate, right?You can run this code in your SSMS and it will work on sqlServer 2016 and newer.
Result:
If you need to round to whole values or halves, it's easiest to multiply the number by two, truncate it, and divide it by two again. You can truncate with the third parameter of
ROUND()
or usingFLOOR()
but the functionality changes on negative values.