SQL Query Mathematic operation [duplicate] - sql

This question already has answers here:
Integer division in sql server
(8 answers)
Closed last year.
I Exec below code in SQL Server Management Studio
SELECT CAST (9/100 AS DECIMAL(8,3))
this should return 0.090 but Return 0.000 .
Who can tell what the problem is and why it gives me this output?

The problem with your current approach is that the 9/100 ratio is computed using integer division before you make the cast to decimal. By the time that cast happens, it is already too late and you've lost the decimal component. Simply I would just do:
SELECT 9 / 100.0; -- 0.090000
So long as the numerator or denominator be a decimal, the quotient will also carry the decimal result.

Related

Rounding off in SQL Server by 2 places after decimal and neglecting 3rd value after decimal [duplicate]

This question already has answers here:
TSQL Round up decimal number
(4 answers)
Closed 2 years ago.
I need to round off a numeric value in SQL Server.
Consider this example - my value is 179.8744.
I want it to be converted to 179.88
I have tried ROUND function, but it only converts if 3rd value is greater than 5. Is there any other function?
Thanks in advance.
You could get creative with the ceiling() function. This function will always round up, but to the next integer. For 2 decimal places this can be resolved by multiplying and dividing by 100.0.
-- move decimal separator up
select 179.8744 * 100.0
-- round up to next integer
select ceiling(179.8744 * 100.0);
-- move decimal separator down (complete formula)
select ceiling(179.8744 * 100.0) / 100.0;
Fiddle
use decimal or round like below!
Examples decimal
select cast(179.8744 as decimal(10,2))
OR
select round(179.8744,2)

How to truncate decimal in Microsoft SQL Server [duplicate]

This question already has answers here:
Truncate (not round) decimal places in SQL Server
(21 answers)
Closed 3 years ago.
The function TRUNCATE works in MySQL but I get an error in Microsoft SQL Server : Incorrect syntax near (
Basically I have list of value under discount. (Some with lots of decimal)
I just want to keep the 2 decimal without rounding.
E.g from 110.975 to 110.97, 10.259 to 10.25. How do I go about doing that?
SELECT TRUNCATE(110.975,2)
You could use round this way
ROUND (110.975 , 2 , 1 )
ROUND ( numeric_expression , length [ ,function ] )
function Is the type of operation to perform. function must be
tinyint, smallint, or int. When function is omitted or has a value of
0 (default), numeric_expression is rounded. When a value other than 0
is specified, numeric_expression is truncated.
https://learn.microsoft.com/en-us/previous-versions/sql/sql-server-2005/ms175003(v=sql.90)?redirectedfrom=MSDN

number rounded down in select statement with equation SQL Server 2012 [duplicate]

This question already has answers here:
Integer division in sql server
(8 answers)
Closed 7 years ago.
If I do
select 18000*13/100000
it returns 2
when it should return 2.34
How do I make it return the correct number?
thanks
select 18000*13/100000.0
Use a float either in numerator or denominator to get a float.
Use a decimal point to avoid integer division:
select 18000.0*13/100000
You can also CAST() as DECIMAL or FLOAT a field, but I like to just use mycol *1.0

How to Truncate the Decimal Places without Rounding Up? [duplicate]

This question already has answers here:
Truncate (not round) decimal places in SQL Server
(21 answers)
Closed 6 years ago.
Assume i have the following decimal value:
4.584406
I need a simple quick way to truncate the decimal without rounding up, so the output would be 4.5
I'm using T-SQL (SQL Server 2005/2008).
Any help will be appreciated.
using the round function you can try this
select round(4.584406, 1, 1)
the output will be
4.5
the key is the third parameter
ROUND ( numeric_expression , length [ ,function ] )
function
Is the type of operation to perform. function must be tinyint,
smallint, or int. When function is
omitted or has a value of 0 (default),
numeric_expression is rounded. When a
value other than 0 is specified,
numeric_expression is truncated.

SQL Server Cast and Rounding

I have in my select clause:
AVG (cast(scale as decimal(5,2)))
I keep getting a number like: 0.6523412897, nothing I do seems to get me to my desired: 0.65.
Basically I want to have a scale of 2 (two decimal places).
Thanks
Cast the average, don't average the cast:
cast(AVG(scale) as decimal(5,2))
Update
Using ROUND() changes the value not the type. Eg. select round(0.12345,2) returns 0.12000 as it should becuase it rounds 0.12345 to 0.12 but keeps the original type with 5 decimals after the point, hence 0.12000. To change the type one must use cast, as in my post.
Try
ROUND(AVG(scale), 2)
It depends upon the database you are using, but ROUND is the answer for SQL Server:
ROUND