252

Using T-SQL and Microsoft SQL Server I would like to specify the number of decimal digits when I do a division between 2 integer numbers like:

select 1/3

That currently returns 0. I would like it to return 0,33.

Something like:

select round(1/3, -2)

But that doesn't work. How can I achieve the desired result?

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
UnDiUdin
  • 14,924
  • 39
  • 151
  • 249

10 Answers10

390

The suggestions from stb and xiowl are fine if you're looking for a constant. If you need to use existing fields or parameters which are integers, you can cast them to be floats first:

SELECT CAST(1 AS float) / CAST(3 AS float)

or

SELECT CAST(MyIntField1 AS float) / CAST(MyIntField2 AS float)
Richard
  • 29,854
  • 11
  • 77
  • 120
89

Because SQL Server performs integer division. Try this:

select 1 * 1.0 / 3

This is helpful when you pass integers as params.

select x * 1.0 / y
xiaowl
  • 5,177
  • 3
  • 27
  • 28
58

It's not necessary to cast both of them. Result datatype for a division is always the one with the higher data type precedence. Thus the solution must be:

SELECT CAST(1 AS float) / 3

or

SELECT 1 / CAST(3 AS float)
robotik
  • 1,837
  • 1
  • 20
  • 26
M.S.
  • 4,283
  • 1
  • 19
  • 42
33

use

select 1/3.0

This will do the job.

stb
  • 3,405
  • 2
  • 17
  • 24
18

I understand that CASTing to FLOAT is not allowed in MySQL and will raise an error when you attempt to CAST(1 AS float) as stated at MySQL dev.

The workaround to this is a simple one. Just do

(1 + 0.0)

Then use ROUND to achieve a specific number of decimal places like

ROUND((1+0.0)/(2+0.0), 3)

The above SQL divides 1 by 2 and returns a float to 3 decimal places, as in it would be 0.500.

One can CAST to the following types: binary, char, date, datetime, decimal, json, nchar, signed, time, and unsigned.

Supreme Dolphin
  • 2,248
  • 1
  • 14
  • 23
  • 5
    MySQL does allow `CAST`, it just doesn't allow casting to `FLOAT`. Cast to `DECIMAL` instead. See e.g. https://stackoverflow.com/questions/7368163/how-can-i-convert-a-string-to-a-float-in-mysql. – markusk Sep 04 '17 at 18:45
9

Looks like this trick works in SQL Server and is shorter (based in previous answers)

SELECT 1.0*MyInt1/MyInt2

Or:

SELECT (1.0*MyInt1)/MyInt2
Troglo
  • 1,497
  • 17
  • 18
  • 2
    I liked this the most, it even looks nice for percentage calculations: `SELECT 100.0 * @Elapsed / @Total` – EliSherer Oct 17 '18 at 17:49
3

Use this

select cast((1*1.00)/3 AS DECIMAL(16,2)) as Result

Here in this sql first convert to float or multiply by 1.00 .Which output will be a float number.Here i consider 2 decimal places. You can choose what you need.

nazmul.3026
  • 918
  • 1
  • 9
  • 20
3

If you came here (just like me) to find the solution for integer value, here is the answer:

CAST(9/2 AS UNSIGNED)

returns 5

trojan
  • 1,465
  • 19
  • 27
1

I was surprised to see select 0.7/0.9 returning 0.8 in Teradata given they're already as floats/decimal numbers! I had to do cast(0.7 as float) to get the output that I was after.

samsamara
  • 4,630
  • 7
  • 36
  • 66
0

When using literals, the best way is to "tell" SQL which type you mean.

if you want a decimal result, add decimal point ".0" to your numbers:

SELECT 1.0 / 3.0
Result
0.333333

if you want a float (real) result, add "e0" to your numbers:

SELECT 1e0 / 3e0
Result
0.333333333333333
Yair Maron
  • 1,860
  • 9
  • 22