remove decimal Place in SQL - sql

Code :
days_between("Created Time","Locked Time")*24
Result table :
Avg Response time (Hours)
2.91
6.00
9.13
3.65
1.17
0.00
0.77
32.47
The table above is the result i get after the code but currenlty I want to remove all decimal place right before i get this result with the days_between("Created Time","Locked Time")*24 i am using, So how can I do this. I tried to use cast and round () but is not working. Can someone help out. Thank you
Try code:
days_between("Created Time","Locked Time")*24 , 0) AS INT ) AS Response_Time

According to your sample data I guess:
with t(d) as (
values (2.91), (6.00), (9.13), (3.65), (1.17), (0.00), (0.77), (32.47)
)
select d, cast(d/24 as decimal(5,2)) from t;

I have a similar query running on MSSQL, and I can do it as:
SELECT TOP 10 a.SizeBytes AS originalValue
,COALESCE(a.SizeBytes, 0.00) / (1024 * 1024) AS defaultOperation
,CAST(COALESCE(a.SizeBytes, 0.00) / (1024 * 1024) AS INT) AS withCast
,FLOOR(COALESCE(a.SizeBytes, 0.00) / (1024 * 1024)) AS withFloor
,ROUND(COALESCE(a.SizeBytes, 0.00) / (1024 * 1024), 0) AS withRound
FROM filesUploaded a
WHERE a.SizeBytes > 0
Resulting in:
Note that I limited my query to the top 10, for speed generating this example, as my table contains millions of rows.
Also my starting value is integer, so I had to add the decimals in order to be able to see decimals. Probably you don't need to do that as the average should already have decimals, but in my case just dividing returned an integer result for the division without using the COALESCE before dividing.

Related

WHERE condition to exclude amounts with decimals ending with '0's or '5's

Objective:
I have a column 'amount' with decimals. I am trying to exclude rows where the amount value ends either with '0's or '5's.
How can I achieve that...
Column type: decimal (7,2)
Ex: numbers to exclude
10.25
11.20
100.00
You probably want this:
WHERE (CAST(your_field * 100 AS INTEGER) % 5) <> 0
But it is hard to tell without more detail on your data type. Also there can be funky rounding issues with floating point values.
An interesting way to do this uses "modular" arithmetic
where col % 0.1 not in (0.00, 0.05)
The % operator works on non-integer bases as well as integer ones.
What I did here is changed the number into a string, trimmed off the trailing blanks, and then reversed the string to take the first character to see if it was no 1 or 5
SELECT * into #test FROM (SELECT CAST(10.25 as decimal(7,2)) as val UNION SELECT 8.21 UNION SELECT 6.00) DQ
select * from #test WHERE LEFT(REVERSE(RTRIM(CAST(val as nvarchar(50)))),1) NOT IN ('5', '0')
drop table #test

How to make SQL query result show with 2 Decimals

I've stumbled into an issue to get this code to show results with only 2 decimals
SELECT
SUM(CAST(
Size * 8 as Decimal)/1024
) as [Size in MB]
FROM
sysfiles
As for now it returns the size of my database with 7 decimals :/
I would advise converting to a decimal format with two places:
select convert(decimal(20, 2), size * 8.0/1024) as [Size in MB]
from sysfiles;
This converts the value to the right type. However, if you want to ensure that the value is displayed to the right precision/scale, then you can use format() or str():
select str(size * 8.0/1024, 20, 2) as [Size in MB]
from sysfiles;
You can cast decimal with just cast(xxx as decimal({length},{precision}))
SELECT
SUM(CAST(
Size * 8 as Decimal(20,2))/1024
) as [Size in MB]
FROM
sysfiles
just cast the sum itself to decimal
SELECT CAST(SUM(CAST(Size * 8 AS DECIMAL(18,2)) / 1024) AS DECIMAL(18,2)) [Size in MB]

Rounding of the numeric values

I want to round of the values of two columns:
select a.region as "Regions",
a.suminsured,2 as "SumInsured" ,
a.suminsured/b.sum*100 as pct
from (
SELECT region, sum(suminsured) as suminsured
FROM "Exposure_commune" group by region
) a,
(select sum(suminsured) FROM "Exposure_commune") b
I want the suminsured and pct columns to come with 2 decimal places. Can someone tell me what I should do?
You can use directly numeric with two parameters. Second parameter for round decimal.
select sum(column_name::numeric(10,2)) from tablename
Use round() with two parameters, which only works for the data type numeric.
While being at it, your query can be simpler and faster:
SELECT region
, round(sum(suminsured), 2) AS suminsured
, round((sum(suminsured) * 100) / sum(sum(suminsured)) OVER (), 2) AS pct
FROM "Exposure_commune"
GROUP BY 1;
You can use sum() as window function to get the total without additional subquery, which is cheaper. Related:
Postgres window function and group by exception
Multiplying first is typically cheaper and more exact (although that barely matters with numeric).
Data type is not numeric
For data types double precision of real
You can ...
just cast to numeric to use the same function.
multiply by 100, cast to integer and divide by 100.0.
multiply by 100 and use the simple round() and devide by 100.
The simple round() with just one parameter works for floating point types as well.
Demonstrating all three variants:
SELECT region
, round(sum(suminsured), 2) AS suminsured
, (sum(suminsured) * 100)::int / 100.0 AS suminsured2
, round(sum(suminsured) * 100) / 100 AS suminsured3
, round((sum(suminsured) * 100) / sum(sum(suminsured)) OVER (), 2) AS pct
, ((sum(suminsured) * 10000) / sum(sum(suminsured)) OVER ())::int / 100.0 AS pct2
, round((sum(suminsured) * 10000) / sum(sum(suminsured)) OVER ()) / 100 AS pct3
FROM "Exposure_commune"
GROUP BY 1;
SQL Fiddle.

SQLServer CAST and divide 2 decimal numbers, followed by multiplication gives too many decimal places

I'm working in SQL Server 2008. I have an alias column that I'm creating via the following formula:
col1 / col2 * some_number
col1 and col2 are nvarchars in my table, but they're really integers. some_number is an integer. If I blindly do col1 / col2, I will get 0 for most. So, I need to cast them as decimals. I want there to be a maximum of 2 decimal places after the decimal point. Here is what I have currently:
CAST(col1 AS DECIMAL(10,2)) / CAST(col2 AS DECIMAL(10,2)) * 100
However, this returns far more decimal places than just 2. How do I fix my code to return just 2 decimal places?
According to the documents the scale of the result is given by:
s2 = max(6, s1 + p2 + 1)
where p2 represents the precision of the numerator and s2 represents scale of the denominator
So when dividing one DECIMAL(10, 2) by another you can substitute in values:
s2 = max(6, 2 + 10 + 1) = 13
Which is corroborated with a simple example:
SELECT CAST(1 AS DECIMAL(10,2)) / CAST(1 AS DECIMAL(10,2))
= 1.0000000000000 -- 13 decimal points
You need to use another cast on your result to reduce the scale:
SELECT CAST(CAST(col1 AS DECIMAL(10,2)) / CAST(col2 AS DECIMAL(10,2)) * 100 AS DECIMAL(10, 2))
I'd also suggest if the data type of you column is nvarchar, "but they're really integers" that you just bite the bullet and alter the column data type.

math expression returns zero

Sorry to ask a dumb question, but this one's got me stumped.
SELECT
81234 / 160000 * 100 AS Try1,
CAST((81234 / 160000 * 100) AS float) AS Try2
The answer is 50.77125 but both values return zero. What's the problem?
Thanks,
Try using a decimal point.
Something like
SELECT
81234 / 160000 * 100 AS Try1,
CAST((81234 / 160000 * 100) AS float) AS Try2,
81234. / 160000. * 100. AS Try3
SQL Fiddle DEMO
From / (Divide) (Transact-SQL)
If an integer dividend is divided by an integer divisor, the result is
an integer that has any fractional part of the result truncated.
you're implementing a division by int
Try this:
SELECT
cast(81234 as float) / cast(160000 as float) * 100
or
SELECT
81234.00 / 160000.00 * 100
the cause of that is that 81234 / 160000 is an integer division so it returns 0, force it to be float like 81234. / 160000 and it will work
Try this
SELECT
81234.00 / 160000 * 100 AS Try1,
CAST((81234.00 / 160000 * 100) AS float) AS Try2
You're dividing a number that is considered an integer. Thus, by adding two decimals it is converted to a decimal number and decimals are displayed.
In your case, the division 81234 / 160000 resulted in a 0, which multiplied by 100 is still 0.
You are doing integer divides. 81234/160000 gives zero. Multiply by 100 and it's still zero.
Try converting to floats before doing the division.
Change your query like below
SELECT
81234.00 / 160000 * 100 AS Try1,
CAST((81234.00 / 160000 * 100) AS float) AS Try2
** 81234.00 / 160000 return 0.5077125 by manual through sql it will think as Integer Division so it will return the value 0.
Actually the value 81234 is integer and if you devide an integer it will return integer only concerting 81234 in to float will return float
SELECT
(CONVERT(FLOAT,81234) / 160000) * 100 AS Try1,
((CONVERT(FLOAT,81234) / 160000 * 100) AS Try2
Thanks
Ashutosh Arya