SQL Server : round and add percent sign - sql

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

Related

SQL to convert a string to decimal where the last field is a negative identifier

Could anyone help me with converting a string to a positive/negative decimal where the negative indicator is the last value in the field. For example, i have '000000012-' and '000001902 '. I need the first value to return -0.12 and the second to return 19.02, when i sum them i should get a sum of 18.90.
At this point i only know how to get the positive values to work using...
SELECT decimal(sum(decimal(mycolumn,10,2)/100),10,2) as Balance
FROM mytable;
In Standard SQL, you can do something like this:
select sum(case when mycolumn like '%-'
then - cast(replace(mycolumn, '-', '') as decimal(10, 2)) / 100.0
else cast(mycolumn as decimal(10, 2)) / 100.0
end)
This should work in most databases.
I was able to get this to work - was over complicating it. Answer below...
SELECT sum(substr(mycolumn,10,1)||decimal(decimal(substr(mycolumn,1,9),9,2)/100,9,2))
FROM mytable;

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

How to correct this T-SQL syntax to get an output with no decimal places?

I am using SQL Server 2016 and I have the following T-SQL code in my query:
CAST(ROUND([Count of Bookings] * 100.0 / SUM([Count of Bookings]) OVER (PARTITION BY [Market Final], [PropertyCode]), 0) AS NVARCHAR(15)) + '%'
An example of the current output of this code is: 40.000000000000%
I was expecting the output to be: 40%
As a note (I don't know if this is relevant): if I change the number in the nvarchar(x) to lower than 15, I get the following error:
Arithmetic overflow error converting expression to data type nvarchar.
Use str() instead of cast():
str(round([Count of Bookings] * 100.0 /
sum([Count of Bookings]) over(PARTITION BY [Market Final], [PropertyCode]
) , 0), 3, 0) + '%'
Actually, I think str() rounds by default (could the documentation be any less clear on this subject?):
str([Count of Bookings] * 100.0 /
sum([Count of Bookings]) over (PARTITION BY [Market Final], [PropertyCode]
), 3, 0) + '%'
The return type of any input of the ROUND() function is depending on the input data type as you can see on MSDN.
This causes your ROUND() to return a data type with a decimal point (in this calculation a float) that you will have to truncate after the conversion to nvarchar (or cast it to an int before).

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