Formatting integers with a decimal place in sql query - sql

I had the table, resulted from a query as follow:
Lasiorhinus 100
Macrotis 100
Myrmecobius 100
Panthera 50
Sarcophilus 100
The query is sth like this:
select round (count(A) / count(B), 1)
But I'd like the numbers to be formatted as follow:
Lasiorhinus 100.0
Macrotis 100.0
Myrmecobius 100.0
Panthera 50.0
Sarcophilus 100.0
Could anyone help me to format the integer 50 => 50.0 with ROUND(number, 1) function? thank you.

One solution (SQL Server) would be:
select format(round(count(A) / count(B), 1), 'N1')
Ref.:
Round()
Format()

Related

remove decimal Place in 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.

SQL Server - round to one decimal - values like 0.84

How to round up decimal value to 1 when this is decimal like 0.5 or 0.84?
You could use CEILING , but a better approach if you have numbers like 367.375000 is to combine CEILING with ROUND like:
select ceiling(round(your_column, 0)) as rounded_nr ;
Result:
rounded_nr
367
While
select ceiling(367.375000) as rounded_nr ;
will return:
rounded_nr
368
and
select round(367.375000, 0) as rounded_nr ;
will return
rounded_nr
367.000000
Check the demo
Just try round(value, decimals)

Sum column only 2 digits after precision (no round)

I have a float type column with numbers that have 6 digits after precision. I want to sum the column only by 2 digits after precision.
For example, I have 1.257868 and 1.258778 as values and I want to get the result of 2.50 as result of sum.
It looks like you want something like:
sum(floor(mycol * 100) / 100)
The expression within the sum() performs the truncation to 2 decimals.
You can use the 3 argument form of round():
select sum(round(mycol, 2, 1))
odbc truncate
select v.val, {fn TRUNCATE(v.val, 2)} as trncted, sum({fn TRUNCATE(v.val, 2)}) over() as sumtrncated
from
(values (1.257868), (1.258778)) as v(val);

Is there a equivalent roundup of excel to sql query?

I what to know if there is equivalent function of roundup of excel in sql query?
Especially for roundup(value,2)
in SQL using round:
select cast(ROUND(350.00/24,2) as decimal(18,2))
result = 14.58
in Excel using roundup
result = 14.59
How can I achieve excel's roundup result in sql?
There is:
select round(12.4343423, 2)
first = number
second = decimal places
OR
SELECT CEILING(111.45) becomes 112
You can do excel like round-up by this query:
SELECT CEILING( (350.00/24) * 100 ) / 100
Result 14.59
You can also write a TSQL function to roundup like excel( as Anthony, answered to similar question) :
CREATE FUNCTION RoundUp(#value float, #places int)
RETURNS float
AS
BEGIN
RETURN SELECT CEILING(#value * POWER(10, #places)) / POWER(10, #places)
END
GO
And you can call this function like this:
SELECT dbo.RoundUp(350.00/24, 2)
Result 14.59

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