Thursday, August 11, 2022

[FIXED] How to format my result for 2 decimals digits after comma

Issue

I'm kind new to SQL world and just started doing queries.

In this case, I'm calculating how much % a number represents of that other number.

This is how I'm doing:

Percentage = ROUND(((NULLIF(SUM(value_1), 0)) / NULLIF(SUM(value_2),0) * 100), 2),

Even if I use ROUND(), the results still the same, they will give me 6 digits after comma:

100.020000
56.800000
-33.330000
100.000000
42.490000

Is there any way to format these numbers with just 2 digits after comma?

Thank you!


Solution

Assuming you use sql server, you can convert your result :

Percentage = Convert(numeric(10, 2), ROUND(((NULLIF(SUM(value_1), 0)) / NULLIF(SUM(value_2),0) * 100), 2))

Or Cast it :

Percentage = Cast(ROUND(((NULLIF(SUM(value_1), 0)) / NULLIF(SUM(value_2),0) * 100), 2) AS numeric(10, 2))


Answered By - EddiGordo
Answer Checked By - Clifford M. (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.