SQL decimal truncation not rounding - sql

Here is the sql i wrote but looking for 5 digits after decimal without rounding.
Select convert(decimal(10,5),Cast(473 as float) / Cast(14322 as float)) --i get 0.03303
Select Cast(473 as float) / Cast(14322 as float) --i get 0.033026113671275
But i'm looking for 0.03302.
Five digits after decimal which (03302) with out rounding.
The first query is rounding it off to 0.3303 instead of 0.03302
Let me know.

Use ROUND with the optional third parameter to truncate:
Select convert(decimal(10,5),
ROUND(Cast(473 as float) / Cast(14322 as float)
,5,1 // the 1 tells SQL to truncate instead of round
))

SELECT CONVERT(DECIMAL(10,5), 473 / (14322 * 1.0) - 0.000005);

Related

Percentage and Decimals

I am trying to convert a varchar to a percentage with a decimal. For example, my report is returning 13590 as a result for a rate, which I would like to have a result of 13.590%. I can't seem to get this to work, any help would be appreciated.
You can use Arithmetic operator inside your query, e.g.
SELECT (report / 100) as rate FROM MyTable;.
If the % is needed, look at the CONCAT function.
Not a very elegant solution, and will drop trailing zeros
SELECT cast(cast(columnA as float) / cast(1000 as float) as varchar(20)) + '%'

how to get rid off decimals from sql results

I am calculating total hours/minutes but i would like to get rid off the decimals and only show something like this 2.00 hours or 2.5 hours etc. I am getting now something like this: 2.000000 and want only to limit to 2 decimals only.
select DATEDIFF(minute, Min(FullDatetime), Max(FullDatetime)) / 60.0 as hours
from myTable
where userid = 123
You can do it by rounding but the easiest is to format for output using FORMAT().
select FORMAT(DATEDIFF(minute, Min(FullDatetime), Max(FullDatetime)) / 60.0, 'N2') as hours
from myTable
where userid = 123
Helpful original documentation: here
try use
cast('values' as decimal(18,2)) --2 decimal place.
select Cast((DATEDIFF(minute, Min(FullDatetime), Max(FullDatetime)) / 60.0 as hours)as decimal(18,2))
from myTable
where userid = 123
There are a few options out there.
I prefer to use the following when no rounding is needed
FORMAT(value, 'N2')
SQL - Rounding off to 2 decimal places
how to get 2 digits after decimal point in tsql?
just use ROUND function such as : SELECT ROUND(columnName,decimals) from table
You could use STR or CONVERT function:
DECLARE #v NUMERIC(19, 6)
SET #v = 2.189189
SELECT STR(#v, 19, 2) AS Method1_Result_VARCHAR, CONVERT(NUMERIC(15, 2), #v) AS Method2_Result_NUMERIC
/*
Method1_Result_VARCHAR Method2_Result_NUMERIC
---------------------- ----------------------
2.19 2.19
*/
Note: First argument of STR function has float type and this means that 1) SQL Server will convert this argument from numeric to float and 2) method 1 uses a non-deterministic expression.

How do i convert minutes to represent hours decimally in sql?

I have Minutes 140. Which in hours and minutes becomes 2:20.
In this case i would love to get 2.33.
What i've tried:
select cast(140/60 as varchar(8)) + '.' + cast((140 % 60) as varchar(8))
Outputs: 2.20
select 140/60
Outputs: 2
select cast(140/60 as decimal(5,2))
Outputs: 2.00
What am i missing?
How do i convert 140 minutes to represent hours decimally?
Sorry about the quick comment, i'll try to explain a little more clearly about this.
By default, Sql will "think" that both your dividend & divisor are INT data type, that's why it returns 2.
If you specify the number with decimal, like this :
select (140.00/60.00)
now the data type is not int any more, and the result is : 2.3333333
So, you will need to convert one of the data type to float, decimal, numeric(n, n) to get the accurate result :
select cast(140 as decimal(5, 2)) / cast(60 as decimal(5, 2))
But you still can just convert only dividend or divisor, like this :
select 140 / cast(60 as decimal(5, 2))
or
select cast(140 as decimal(5, 2)) / 60
they both gave the same result, becasue the result type is the data type of the argument with the higher precedence, in this case, decimal has the higher precedence than int
you can read more here :
Divide
Data Type Precedence
Try this
Select convert(decimal(5,2),convert(float,140)/60)
or
Select cast(140.00/60 as decimal(5,2))

Remove extra Zeros in percentage in SQL

I have a query to get % from two summed columns. It gives me correct % but the zeros are not going off, even if I use round function. The result like this 95.40000
how I can remove the extra zeros.
Select (Round((COUNT(Id * 100) / Total,1)) AS Percentage
As bAN, use cast(), but cast as float :
Select cast((Round((COUNT(Id * 100) / Total,1)) as float) AS Percentage
Use CAST()
Exemple:
Select ( (CAST(NUMBER/100 AS DECIMAL(10,2)))) AS Percentage
from MyTable
In your case:
Select CAST((Round((COUNT(Id * 100) / Total,1)) as AS DECIMAL(10,2)) AS Percentage
from YourTable

SQL rounding to 7 places, even though its set as decimal(18,4))

select (cast(datediff(minute,'1900-01-01 07:03:00.000' ,'1900-01-01 10:35:00.000')
as decimal(18,4))/60)
Even though the decimal places are set to 4, it returns
3.5333333
instead of
3.5333
Please help me return 3.5333 (4 decimal places)
http://sqlfiddle.com/#!3/1fa93/4119/0
Put the /60 in the cast and change it to 60.0 to have a decimals.
select (cast(datediff(minute,'1900-01-01 07:03:00.000' ,'1900-01-01 10:35:00.000')/60.0
as decimal(18,4)))
You need to do your division before casting:
select
cast(datediff(minute, '1900-01-01 07:03:00.000', '1900-01-01 10:35:00.000') / 60.0
as decimal(18,4))
Then you get:
3.5333
Casting to decimal(18,4) and then doing a division by 60 sort of "invalidates" your formatting - SQL Server will show all the numeric precision of that division by 60 again...