matt80
matt80

Reputation: 35

Trying to round a value which results from 2 multiplied fields

In SQL Server, I am trying to round a value which results from 2 multiplied fields t.hourlyRate and t.hours. The sum is calculated as:

SUM(t.hours * t.hourlyRate) AS tot_cost

The results gives me values such 33.2330 or 51.6648 whereas I just want 33.2 or 51.7.

I've just variants of cast/round etc to no avail - any ideas please

Upvotes: 2

Views: 53

Answers (3)

Barbaros Özhan
Barbaros Özhan

Reputation: 65373

Use ROUND function, which starts with SQL Server 2008 , upto precision 1 as :

SELECT ROUND(33.2330, 1) AS RoundValue1,
       ROUND(51.6648, 1) AS RoundValue2;

RoundValue1    RoundValue2
-----------    -----------
   33.2           51.7 

Upvotes: 2

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522396

If you are using SQL Server 2012 or later, then FORMAT is an option:

FORMAT(SUM(t.hours * t.hourlyRate), 'N1') AS tot_cost

Upvotes: 0

juergen d
juergen d

Reputation: 204884

You can cast() the result like this

cast(sum(t.hours * t.hourlyRate) as decimal(10,1))

Upvotes: 2

Related Questions