Percentage and Decimals - sql

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

Related

Rounding DOWN postgresql

Here is a example query, I've been trying to find a workaround for this but my knowledge for postgresql at this time is still limited. Thanks in advance.
Sample Query:
select round(3.041,2) as column
Expected Output:
3.03 instead of 3.04
Rounding UP works but I need it to round DOWN as well if the decimal value is <5.
You can round down with some simple decimal counting like
SELECT
3.04 - POWER(10,
(-1 *
CAST(
LENGTH(
SPLIT_PART(
CAST(3.04 AS STRING), '.', 2
)
)
AS INT)
)
)
However, I have no idea why you would do this. Are you sure you aren't trying to use the basic ROUND function and cut off more significant figures?

How to Cast values to decimal in Hive

This is example of the column "Amount" i am working on. I need to remove all '+' and '-' and all leading zeros, then convert to decimal. There are also cases when there is only zeros present and i am not sure what is the best approach for it. Is there a better solution? Please help on this.
Amount
-0000211
+0000101
+0000000
+0000013
CAST(CAST(CAST(Amount as INT) AS varchar(8)) as decimal(8,2)) from payment_table;
use abs() to remove +/- and 0, and then use cast() to cast to decimal.
select CAST( ABS(Amount) as DECIMAL(8,2)) as ret_Amount -- this will return 211.00

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;

Remove decimal using SQL query

I want to convert my decimal SQL query result in percent. Example I have a 0.295333 I want it to be 30% and if I have a 0.090036 I want it to be 9%.
This is what I have so far.
(100 * (sample1/ sample2) ) as 'Percent'
I also tried this one but the problem is result comes with ".00" and I don't know how to remove the decimal.
cast (ROUND(100 * (sample1 / sample2),0) As int ) as 'Percent'
Try with the below script..
cast (100 * Round((sample1 / sample2),2) As int ) as 'Percent'
So as some of the comments pointed out you may need to pay attention to your datatype if one or both of the original columns that you get your decimal from are integer.
One easy way of dealing with that is something like this:
ColA * ColB * 1.0 which will make sure that your integers are treated as decimals
So if you have SQL Server 2012+ you can use Format and not mess with rounding at all. Like this FORMAT(YourDecimal,'#%'), yep that simple.
;WITH cteValues AS (
SELECT 0.295333 as OriginalValue
UNION ALL
SELECT 0.090036 as OriginalValue
)
SELECT
OriginalValue
,FORMAT(OriginalValue,'#%') as PercentFormat
FROm
cteValues
If you are pre 2012 and do not have format an easy way is to round to the 100th then times by 100 and cast as int CAST(ROUND(YourDecimal,2) * 100 AS INT)
;WITH cteValues AS (
SELECT 0.295333 as OriginalValue
UNION ALL
SELECT 0.090036 as OriginalValue
)
SELECT
OriginalValue
,CAST(ROUND(OriginalValue,2) * 100 AS INT) as PercentInt
FROm
cteValues
Because an INT cannot by definition have decimal places, if you are receiving .00 with the method similar to this or the one you have tried, I would ask the following.
Are you combining (multiplying etc.) the value after casting with another column or value that may be decimal, numeric, or float?
Are you looking at the query results in a program outside of SSMS that could be formatting the results automatically, e.g. Excel, Access?
Address your assumptions first.
How does ROUND work? Does it guarantee return values and if so, how? What is the precedence of the two columns? Does Arithmetic operators influence the results and how?
I only know what I do not know, and any doubt is worth an investigation.
THE DIVIDEND OPERATOR
Since ROUND always returns the higher precedence, this is not the problem. It is in fact the divide operator ( / ) that may be transforming your values to an integer.
Always verify the variables are consistently of one datatype or CAST if either unsure or unable to guarantee (such as insufficiently formatted. I.e. DECIMAL(4,2) instead of required DECIMAL(5,3) ).
DECLARE #Sample1 INT
, #Sample2 DECIMAL(4,2);
SET #Sample1 = 50;
SET #Sample2 =83.11;
SELECT ROUND( 100 * #Sample1 / #Sample2 , 0 )
Returns properly 60.
SELECT ROUND( 100 * #Sample2 / #Sample1 , 0)
Incorrectly turns variables into integers before rounding.
The reason is that DIVIDE - MSDN in SQL may return the higher precedence, but any dividend that is an integer returns another integer.
UPDATE
This also explains why the decimal remains after ROUND...it is of higher precedence. You can add another cast to transform the non-INT datatype to the preferred format.
SELECT CAST( ROUND( <expression>, <Length>) AS INT)
Note that in answering your question I learned something myself! :)
Hope this helps.

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