Issue with DateDiff / Nb in SQL Server [duplicate] - sql

This question already has answers here:
Integer division in sql server
(8 answers)
Closed 8 years ago.
When I use a DateDiff in a SELECT statement in SQL Server, I get an int value back:
select DATEDIFF(day,'01/01/2011','15/05/2011')
from Table1
Result is : 134
select DATEDIFF(day,'01/01/2011','15/05/2011')/15
from Table1
Result is : 8
select cast(DATEDIFF(day,'01/01/2011','15/05/2011') / 15 as Decimal(9,2))
from Table1
Result is : 8.00
But in reality the result is : 8.9333333
How can I change the result from int to Double?
I want to get the result 8.9333333 - how can I do this?

The problem is that you're using CAST after dividing. If you perform the division after the CAST, you'll get the answer you want:
select
cast(DATEDIFF(day,'01/01/2011','15/05/2011') as Decimal(9,2))/15
from Table1

You need to cast your inputs into the division as decimal first:
select cast(DATEDIFF(day,'2011-01-01','2011-05-15') as Decimal(9,2))/15.0
Otherwise you are dividing an integer by an integer - you'll get an integer out. If you then cast this to a decimal after the division, you are going to get 8.0.

SELECT CAST(DATEDIFF(day,'01/01/2011','15/05/2011') AS Decimal(9,2))/15
FROM Table1

Related

How to round off the below values in SQL Server? [duplicate]

This question already has an answer here:
How to lower bound the value 6.15 to 6.1 in SQL Server?
(1 answer)
Closed 4 years ago.
How to round off the below values in SQL?
35.38
53.08
6.15
5.38
to
35.4
53.1
6.1
5.4
SELECT ROUND(colname - 0.01, 1) AS RoundValue FROM TableName;
SYNTAX OF ROUND():
ROUND ( numeric_expression , length [ ,function ] )
Demo
http://sqlfiddle.com/#!18/2f9d9/11
Have you tried round()?
select round(val, 1)
However, I would recomend converting to a decimal format:
select cast(val as decimal(10, 1))
The cast() not only changes the value, but it also changes the type. That means that tools should also print out only one decimal place.

MSSQL - Getting decimals in a division [duplicate]

This question already has answers here:
How to get a float result by dividing two integer values using T-SQL?
(10 answers)
Decimal values in SQL for dividing results
(6 answers)
Closed 6 years ago.
I'm using MSSQL 2014 and I'd like to know if there is a better way to display 2 decimals in a simple division like 10/3.
SELECT 10/3 -- returns 3
SELECT CONVERT(DECIMAL(10, 2), 10/3) -- RETURNS 3
SELECT CAST(10/3 AS DECIMAL(10,2)) -- RETURNS 3
The only way I found to make it work is by casting the divisor as float:
SELECT 10/CAST(3 AS FLOAT) -- returns 3.333333...
SELECT CONVERT(DECIMAL(10, 2), 10/CAST(3 AS FLOAT)) -- RETURNS 3.33
SELECT CAST(10/CAST(3 AS FLOAT) AS DECIMAL(10,2)) -- RETURNS 3.33
Are these two last options the best approach available?. Is it possible to do this without any cast/conversion?
Yeah, to truncate you have to specify that you only want two digits. If it were me I'd do:
SELECT cast(10/3.0 as decimal(10,2))
I get a float if I do,
select 10/3.0
https://stackoverflow.com/a/11719098/28045
Division of one integer by another will result in an integer. Either cast the numbers or use variables of float or decimal type having values of 10 and 3.

how to get rid off decimals from sql results

I am calculating total hours/minutes but i would like to get rid off the decimals and only show something like this 2.00 hours or 2.5 hours etc. I am getting now something like this: 2.000000 and want only to limit to 2 decimals only.
select DATEDIFF(minute, Min(FullDatetime), Max(FullDatetime)) / 60.0 as hours
from myTable
where userid = 123
You can do it by rounding but the easiest is to format for output using FORMAT().
select FORMAT(DATEDIFF(minute, Min(FullDatetime), Max(FullDatetime)) / 60.0, 'N2') as hours
from myTable
where userid = 123
Helpful original documentation: here
try use
cast('values' as decimal(18,2)) --2 decimal place.
select Cast((DATEDIFF(minute, Min(FullDatetime), Max(FullDatetime)) / 60.0 as hours)as decimal(18,2))
from myTable
where userid = 123
There are a few options out there.
I prefer to use the following when no rounding is needed
FORMAT(value, 'N2')
SQL - Rounding off to 2 decimal places
how to get 2 digits after decimal point in tsql?
just use ROUND function such as : SELECT ROUND(columnName,decimals) from table
You could use STR or CONVERT function:
DECLARE #v NUMERIC(19, 6)
SET #v = 2.189189
SELECT STR(#v, 19, 2) AS Method1_Result_VARCHAR, CONVERT(NUMERIC(15, 2), #v) AS Method2_Result_NUMERIC
/*
Method1_Result_VARCHAR Method2_Result_NUMERIC
---------------------- ----------------------
2.19 2.19
*/
Note: First argument of STR function has float type and this means that 1) SQL Server will convert this argument from numeric to float and 2) method 1 uses a non-deterministic expression.

control in display Decimal separators in sql server

I have problem in display the decimal numbers, it has many number.
My sql statement :
Select sum(HS$totalMoney)
the result :
12132.123444343
I want to display as 12132.12 without the another number
Thanks.
If your logic is for money you should first round the values not truncate
select CONVERT(decimal(18,2),round(12132.123444343 ,2)) gives 12132.12
select CONVERT(decimal(18,2),round(12132.125944343 ,2)) gives 12132.13
Try this one -
SELECT CAST(12132.123444343 AS DECIMAL(10,2))
if you are using mysql, use code blew
SELECT TRUNCATE(sum(HS$totalMoney), 2);
this query slove your problem
SELECT CAST(12132.123444343 AS DECIMAL(10,2))
or you can use
select CONVERT(decimal(18,2),round(12132.1255555 ,2))
SELECT CONVERT(decimal(21, 2), sum(HS$totalMoney))
-- This one will round in SQL Server but truncate in ASE 15 (which was available to me at the time)
SELECT CONVERT(decimal(21, 2), round(sum(HS$totalMoney), 2, 1))
-- This one uses a variant of ROUND supported by SQL Server, but not ASE 15 (and will truncate the third and subsequent decimal places).
The round function has a function parameter to truncate instead of round:
select round(12132.123444343 , 2, 1)
From here:
http://msdn.microsoft.com/en-us/library/ms175003.aspx

Sql query to convert nvarchar to int

I have to query for total amount of a column using an aggregate function. The column data type is NVARCHAR(MAX). How can I convert it to Integer?
I have tried this:
SELECT SUM(CAST(amount AS INT)),
branch
FROM tblproducts
WHERE id = 4
GROUP BY branch
...but I'm getting:
Conversion failed when converting the nvarchar value '3600.00' to data type int.
3600.00 is not integer so CAST via float first
sum(CAST(CAST(amount AS float) AS INT))
Edit:
Why float?
no idea of precision or scale across all rows: float is the lesser evil perhaps
empty string will cast to zero for float, fails on decimal
float accepts stuff like 5E-02, fails on decimal
In addition to gbn's answer, you need to protect against non-numeric cases:
sum(CASE WHEN ISNUMERIC(Amount)=1 THEN CAST(CAST(amount AS float) AS INT)END )
SELECT sum(Try_Parse(amount as Int Using 'en-US')),
branch
FROM tblproducts
WHERE id = 4
GROUP BY branch