Remove extra Zeros in percentage in SQL - 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

Related

What's the difference between these 2 SQL query involving round() and ceiling ()?

SELECT ROUND(CEILING(36.3) / 4,2)
SELECT ROUND(37 / 4,2)
The first query returns 9.25. The second query returns 9.
Ceiling(36.3) would have returned 37. Why would there be such differences then?
The difference is due to the return type from operations. When you call Ceiling on any number or expression, the return type is of same type as the expression.
Let's look at the first one:
Ceiling(36.3) -> returns a decimal
This means that the division operation is also done on decimals which means you get a decimal result and thus could be rounded.
In the second statement, you are dividing 2 integers which would also result in a integer result (just the quotient). Thus the value will only be 9.
SELECT ROUND(CEILING(36.3) / 4,2) -- select round(decimal/int,int) => select round(decimal, int)
SELECT ROUND(37 / 4,2) -- select round(int/int,int) => select round(int, int)
You would get similar result when you do this:
SELECT ROUND((CEILING(36.3) / 4),2)
SELECT ROUND((cast(37 as decimal(10,2)) / 4),2)

SELECT SUM for time

I have a Table in SQL server with a column "Time" having data type as time(7). Need to call the sum of this column, and when I use the following statement, it returns result as integer only.
Eg. If total time is 1:30:00,I expect result as 1.5. But the code I use doesn't get me this, it get me result as 1. Please check if you have a solution.
The code I used is
SELECT SUM(DATEPART(ss,Time) + DATEPART(mi,Time)*60 + DATEPART(hh,Time)*3600)/3600 AS TotalTime FROM dbo.Table
SELECT (
DATEPART(hh,Time) +
DATEPART(mi,Time) / 60.0 +
DATEPART(ss,Time) / 3600.0
) AS TotalTime
FROM dbo.Table
Try below - you don't need sum() function here and in your case, it is showing 1 because your result is 5400/3600 which is 1 but you need to add a float value as you are expecting float result
SELECT (DATEPART(ss,'1:30:00') + DATEPART(mi,'1:30:00')*60 +
DATEPART(hh,'1:30:00')*3600)/3600.00
AS TotalTime FROM dbo.Table
Try this, you can change the datepart argument based on your needs here is the full list
SELECT SUM(CAST(DATEDIFF(MINUTE, '00:00:00', [Time]) as float)/60) AS TotalHours FROM [dbo].[Table]
When you divide some value by int type, the result will be also int (the fraction is just dropped). Therefore, you need to convert a divider of 3600 from int to decimal:
SELECT SUM(DATEPART(ss,Time) + DATEPART(mi,Time)*60 + DATEPART(hh,Time)*3600)/CONVERT(DECIMAL(16,4), 3600) AS TotalTime FROM dbo.Table
If you want the difference in decimal hours, then do the following:
Convert the time values to seconds.
Sum the seconds.
Divide by 60 * 60
So:
select sum(datediff(second, 0, v.t)) / (60.0 * 60)
from (values (convert(time, '00:10:01')),
(convert(time, '01:00:03'))
) v(t)
There is no reason to break the value in to component parts. That just seems unnecessarily complicated.

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)) + '%'

SQL Server : round and add percent sign

I have the following SQL query that is returning a result of 92.967013425802 and I need it to be formatted like 93% and add the percent sign. I have tried changing the sum to round but I received an error
The function 'round' is not a valid windowing function, and cannot be used with the OVER clause.
My query:
select
count(*) * 100.0 / sum(count(*)) over()
from
db_table_MetaData
group by
MetaValue
order by
MetaValue
Any help would be appreciated.
select
--Cast(Round(count(*) * 100.0 / sum(count(*)), 0) as nvarchar(5) + '%'
CAST(Round(count(*) * 100.0 / sum(count(*)), 0) as nvarchar(5)) + '%'
from
db_table_MetaData
This should do the trick.
In essence you take the 08/15 ROUND() function to get your numeric value. After this you cast it into a nvarchar(x) and add a string to your string. However I have no method of checking my syntax right now.
Strange you got not a valid function. Perhaps you didn't provide the correct parameters?
This worked for me.
select cast(Round('92.967013425802', 0) as nvarchar(10)) + '%'
I don't know what the real issue is, but when I tried the below query it worked fine. May be its the placement of parenthesis which went wrong with your query,
select
MetaValue, round(count(*) * 100.0 / sum(count(*)) over(),0)
from db_table_MetaData
group by MetaValue
order by MetaValue
check it working at SQL Fiddle: http://www.sqlfiddle.com/#!3/55c82/3
Answer (count(*) * 100.0 / sum(count(*)) over(),1)
I would use the FORMAT function with parameter 'p':
SELECT FORMAT(50.019/100.0,'p') as [Percentage]
This will give you a rounded result like:
Percentage
50.02 %
Check MSDN for more infos:
https://msdn.microsoft.com/de-de/library/hh213505.aspx
select
CONCAT(count(*) * 100.0 / sum(count(*)), '%') AS Percentage
from
db_table_MetaData
group by
MetaValue
order by
MetaValue

SQL decimal truncation not rounding

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);