SQL Server - round to one decimal - values like 0.84 - sql

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)

Related

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

SQL Cast to show decimals

I have the following statement within my select clause;
(([Complt_Emp] + [No_Non_Complt_Emp])/ [No_of_Emp]) as Total_Completed
How do I implement CAST "cast(your_float_column as decimal(10,2))" ? I want my column Total_Completed to show 2 decimal places
I cannot seem to get the correct syntax!
Thank you
the result of the calculation depends of the used columns type.
If you divide int columns, you get int result : 1 / 6 = 0
when you convert each values to decimal the result is: 1 / 6 = 0.1666666666666
Now you want 2 decimal result,so you have to convert/ round the previous result to get the expected value
See fiddle for some example of divide and cast / round : http://sqlfiddle.com/#!18/51785/5
An easy trick can be to use :
round ( 1.0 * ( [Complt_Emp] + [No_Non_Complt_Emp] ) / [No_of_Emp] , 2 )
Cast each expression seperately
CAST(([Complt_Emp] + [No_Non_Complt_Emp]) as decimal(10,2)) /
CAST([No_of_Emp] as decimal(10,2)) as Total_Completed
I suspect all your values are INT. An int divided by an int will return an int.
(([Complt_Emp] + [No_Non_Complt_Emp])/ cast([No_of_Emp] as decimal(10,2)) as Total_Completed
Try this
cast((([Complt_Emp] + [No_Non_Complt_Emp])/ [No_of_Emp]) as decimal(10,2)) as Total_Completed

Formatting integers with a decimal place in sql query

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

CEILING in SQL does not return correct values

CEILING in T-SQL does not return correct values in SQL Server 2012. Please see below details.
select CEILING(0.3333333333333333)
Result: 1
But I want to do a calculation of based on count of columns. Internal calculation is given below.
select CEILING((17 - 2) / 45)
Result: 0
(17 - 2) / 45 = 0.33333
But
select CEILING((17 - 2) / 45)
returns zero
I want to get one as result. What should I do?
It ended with a integer division. So
15/45 = 0
Try CASTing atleast one of the operand into decimal ,
SELECT CEILING((17-2)/CAST(45 AS DECIMAL (4,2))
OR like,
SELECT CEILING(15,45.0)

How I can get the first 3 digits in 123456 Numbers in sql?

I have field called CallingParty in My CDR table it contains data like this:
CallingParty
------------
267672668788
I want to select the first 3 number of each of those numbers like
CallingParty
------------
267
if CallingParty is of type int:
SELECT CAST(LEFT(CallingParty, 3) AS INT)
From CDR
SQL Server has a Left() function, but it works best on strings. (varchar/char in SQL)
Select left(cast(267672668788 as varchar), 3)
Use this query:
SELECT SUBSTRING(CAST(CallingParty AS VARCHAR(50)), 1, 3) FROM [CDR]
If the data length does not change then you can always divide by 10 * the digits you have
SELECT FLOOR(267672668788 / 1000000000)
=267
Try this:
SELECT Substring(callingparty, 1, Length(callingparty) - 9)
FROM cdr;