October 26, 2014 at 9:21 am
I run the following statement: select 1/3 and I got 0.
How do I do so that I can get 0.33?
Thanks,
Jian
October 26, 2014 at 10:53 am
QQ-485619 (10/26/2014)
I run the following statement: select 1/3 and I got 0.How do I do so that I can get 0.33?
Thanks,
Jian
SQL Server performs an Integer division on whole numbers, if you don't want integer division then add a decimal separator.
😎
SELECT 1./3.
Results
0.333333
October 26, 2014 at 12:27 pm
what if that is a column name, e.g.
select sum(saletotal)/NumberofDays
October 26, 2014 at 1:14 pm
QQ-485619 (10/26/2014)
what if that is a column name, e.g.select sum(saletotal)/NumberofDays
Either to an explicit type cast to numeric or float using cast or convert or multiply the column value with 1.0, here's a quick example
😎
USE tempdb;
GO
SET NOCOUNT ON;
;WITH SAMPLE_DATA(DIVIDEND,DIVISOR) AS
(
SELECT 1 , 3 UNION ALL
SELECT 2 , 5 UNION ALL
SELECT 3 , 7 UNION ALL
SELECT 4 , 11 UNION ALL
SELECT 5 , 13 UNION ALL
SELECT 6 , 17 UNION ALL
SELECT 7 , 23
)
SELECT
DIVIDEND
,DIVISOR
,DIVIDEND / DIVISOR AS INTEGER_DIVISION
,(DIVIDEND * 1.0) / DIVISOR AS NOT_INTEGER_DIVISION
FROM SAMPLE_DATA SD;
Results
DIVIDEND DIVISOR INTEGER_DIVISION NOT_INTEGER_DIVISION
----------- ----------- ---------------- ---------------------------------------
1 3 0 0.333333333333
2 5 0 0.400000000000
3 7 0 0.428571428571
4 11 0 0.363636363636
5 13 0 0.384615384615
6 17 0 0.352941176470
7 23 0 0.304347826086
October 26, 2014 at 6:10 pm
QQ-485619 (10/26/2014)
what if that is a column name, e.g.select sum(saletotal)/NumberofDays
select sum(saletotal)/NumberofDays+0.0
--Jeff Moden
Change is inevitable... Change for the better is not.
October 26, 2014 at 8:36 pm
Jeff Moden (10/26/2014)
QQ-485619 (10/26/2014)
what if that is a column name, e.g.select sum(saletotal)/NumberofDays
select sum(saletotal)/NumberofDays+0.0
Actually: select sum(saletotal)/(NumberofDays+0.0) -- << Remember order of precedence for mathematics.
October 26, 2014 at 9:53 pm
Thank you very much for all your help. It works beautifully...
October 26, 2014 at 11:13 pm
Lynn Pettis (10/26/2014)
Jeff Moden (10/26/2014)
QQ-485619 (10/26/2014)
what if that is a column name, e.g.select sum(saletotal)/NumberofDays
select sum(saletotal)/NumberofDays+0.0
Actually: select sum(saletotal)/(NumberofDays+0.0) -- << Remember order of precedence for mathematics.
COFFEE!!! I NEED COFFEE!!! 😀 Thanks for the catch, Lynn,
--Jeff Moden
Change is inevitable... Change for the better is not.
October 27, 2014 at 10:28 am
Jeff Moden (10/26/2014)
Lynn Pettis (10/26/2014)
Jeff Moden (10/26/2014)
QQ-485619 (10/26/2014)
what if that is a column name, e.g.select sum(saletotal)/NumberofDays
select sum(saletotal)/NumberofDays+0.0
Actually: select sum(saletotal)/(NumberofDays+0.0) -- << Remember order of precedence for mathematics.
COFFEE!!! I NEED COFFEE!!! 😀 Thanks for the catch, Lynn,
Well, after seeing people getting the facebook posts of this wrong, it just jumps out at me now:
? = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 X 0 + 1
October 28, 2014 at 1:06 pm
select left(cast(sum(saletotal) as money) / NumberofDays, CHARINDEX('.', cast(cast(sum(saletotal) as money) / NumberofDays as varchar(max)), 1) + 2)
Don Simpson
October 30, 2014 at 4:39 pm
select CAST(sum(saletotal) as money) /NumberofDays;
Should give you the correct significant digits.
----------------------------------------------------
October 31, 2014 at 8:24 am
DonlSimpson (10/28/2014)
select left(cast(sum(saletotal) as money) / NumberofDays, CHARINDEX('.', cast(cast(sum(saletotal) as money) / NumberofDays as varchar(max)), 1) + 2)
A lot of work to round a value to 2 decimal points. Try this:
select round(sum(saletotal) * 1.0 / NumberOfDays, 2);
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply