55

A column height is integer type in my SQL Server table. I want to do a division and have the result as decimal in my query:

Select (height/10) as HeightDecimal

How do I cast so that the HeightDecimal is no longer integer? Thanks.

5 Answers 5

84
SELECT height/10.0 AS HeightDecimal FROM dbo.whatever;

If you want a specific precision scale, then say so:

SELECT CONVERT(DECIMAL(16,4), height/10.0) AS HeightDecimal
  FROM dbo.whatever;
3
  • 8
    +1 for the first part, but beware of the second example. This CONVERT is applied post-divide and cannot give you more than the 6 decimal places that you already have. Dec 17, 2012 at 12:30
  • perfect solution!! I guess first one performs better! isn't it?
    – Diablo
    May 9, 2016 at 8:40
  • 1
    @Diablo Well, you could test it, but I doubt you'll notice any difference. Anyway, if you need specific precision/scale, do that formatting in your application layer. May 9, 2016 at 14:33
23
SELECT CAST(height AS DECIMAL(18,0)) / 10

Edit: How this works under the hood?

The result type is the same as the type of both arguments, or, if they are different, it is determined by the data type precedence table. You can therefore cast either argument to something non-integral.

Now DECIMAL(18,0), or you could equivalently write just DECIMAL, is still a kind of integer type, because that default scale of 0 means "no digits to the right of the decimal point". So a cast to it might in different circumstances work well for rounding to integers - the opposite of what we are trying to accomplish.

However, DECIMALs have their own rules for everything. They are generally non-integers, but always exact numerics. The result type of the DECIMAL division that we forced to occur is determined specially to be, in our case, DECIMAL(29,11). The result of the division will therefore be rounded to 11 places which is no concern for division by 10, but the rounding becomes observable when dividing by 3. You can control the amount of rounding by manipulating the scale of the left hand operand. You can also round more, but not less, by placing another ROUND or CAST operation around the whole expression.

Identical mechanics governs the simpler and nicer solution in the accepted answer:

  SELECT height / 10.0

In this case, the type of the divisor is DECIMAL(3,1) and the type of the result is DECIMAL(17,6). Try dividing by 3 and observe the difference in rounding.

If you just hate all this talk of precisions and scales, and just want SQL server to perform all calculations in good old double precision floating point arithmetics from some point on, you can force that, too:

SELECT height / CAST(10 AS FLOAT(53))

or equivalently just

SELECT height / CAST (10 AS FLOAT)
1
  • Thank you for such a detailed explanation of Precision/Scale/Length, and for including such useful links! Really appreciate it! :) Aug 27, 2021 at 21:10
4

You can either cast Height as a decimal:

select cast(@height as decimal(10, 5))/10 as heightdecimal

or you place a decimal point in your value you are dividing by:

declare @height int
set @height = 1023

select @height/10.0 as heightdecimal

see sqlfiddle with an example

0
2
select cast (height as decimal)/10 as HeightDecimal
5
  • decimal(anything,anything)? Why not specify? Apr 22, 2012 at 20:10
  • You are right about division, but can produce overflow problem for example for multiplication. decimal(h)*10 in general may not be equal to decimal(g*10), thinking such way I have brought up the habit to cast first before operation
    – Dewfy
    Apr 22, 2012 at 20:25
  • 3
    I agree an explicit cast before operations is better. But my point was that if you're going to say decimal you should say decimal(precision,scale). Apr 22, 2012 at 20:58
  • @AaronBertrand - It depends on circumstances. I'd agree with you when specifying database schema (especially stored procedure parameters). However, the burden of proof of usefulness, is on the side arguing presence, not on absence, of particular piece of code. Apr 23, 2012 at 8:26
  • 1
    If I'm the app developer and I need to consume your query and/or write an interface to it, wouldn't you rather specify your intent for the data type so I know what to expect, rather than have me go through a bunch of tests and/or track you down and ask you? Do you also leave out the length specification for char/varchar/nchar/nvarchar for similar reasons? Apr 23, 2012 at 13:26
0
declare @xx int 
set     @xx = 3 
select @xx      
select @xx * 2  -- yields another integer  
select @xx/1    -- same
select @xx/1.0  --yields 6 decimal places 
select @xx/1.00 --       6 
select @xx * 1.0  --     1 decimal place - victory
select @xx * 1.00 --     2         places - hooray 

Also _ inserting an int into a temp_table with like decimal(10,3) _ works ok.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.