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

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

Related

SQL Query Mathematic operation [duplicate]

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.

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

CAST from number to varchar is returning exponential value? [duplicate]

This question already has answers here:
How to convert float to varchar in SQL Server
(15 answers)
Closed 8 years ago.
I am trying a number column to varchar in SQL select statement. It is returning in exponential value. How can I get as it is in string
Select CAST([Loan Number] as nVARCHAR(50)) as strLoanNumber
From
[tbl_DS] Where [Loan] like '%CWALT_2006%'
Actual value is 127160640
Returning value is 1.27161e+008
Try using
convert(nvarchar(50),[loan number])

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.