x
login about faq Site discussion (meta-askssc)

Cast to decimal

This is probably an easy fix but I can't seem to get it working.

select columnA, cast( (columnaA/10) as decimal  (10, 5)) as NewColumn 
 from tableA

I want to divide column A by 10 but I want my new column to show the decimal places. Right now it is showing 5 deciaml places but only as zero.

When I divide 3/10 I want the new column to show 0.3 but right now its just showing 0.00000

more ▼

asked Oct 11 '11 at 08:39 AM in Default

sqlnewb gravatar image

sqlnewb
216 10 27 31

(comments are locked)
10|1200 characters needed characters left

3 answers: sort voted first

Cast the column before the divide

select columnA, cast(columnA as decimal  (10, 5))/10 as NewColumn 
from tableA

without it, it is doing integer division 3/10 = 0 then casting that as decimal(10,5) = 0.00000

more ▼

answered Oct 11 '11 at 08:44 AM

Kev Riley gravatar image

Kev Riley ♦♦
46k 38 43 69

@Kev Riley But this will definitely result in more than 5 decimal digits in the answer, so using 10.0 instead of 10 and only then casting to decimal(10, 5) could be a better choice.

Oct 11 '11 at 08:47 AM Oleg

@Oleg yes it does, but I was just trying to show the OP where they had gone wrong - hopefully it will serve as a learning exercise and the OP can rework the query to suit their requirements. If 10 wasn't hard-coded and was an int variable or another column, changing it to 10.0 isn't as easy, and you would be back to casting/converting either the divisor or the dividend to a decimal type.

Hopefully all the answers here show that :)

Oct 11 '11 at 08:57 AM Kev Riley ♦♦
(comments are locked)
10|1200 characters needed characters left

This happens because your ColumnA is int and 10 is implicitly int as well. Try this instead, and you will get results you want:

select 
    columnA, cast((columnaA/10.0) as decimal  (10, 5)) as NewColumn 
    from tableA

The idea is that when you divide by 10.0 rather than by 10 then you avoid the integer division because the decimals are higher in the food chain than integers.

Oleg

more ▼

answered Oct 11 '11 at 08:44 AM

Oleg gravatar image

Oleg
15.4k 1 4 24

(comments are locked)
10|1200 characters needed characters left

You need to cast each number to decimal as well as the total, for example:

select columnA, CAST(CAST(columnaA AS DECIMAL(10,5)) / CAST(10.0 AS DECIMAL(10,5)) AS DECIMAL(10,5))as NewColumn 
 from tableA
more ▼

answered Oct 11 '11 at 08:45 AM

Mrs_Fatherjack gravatar image

Mrs_Fatherjack
4.6k 43 52 60

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x1601
x224
x22

asked: Oct 11 '11 at 08:39 AM

Seen: 1041 times

Last Updated: Oct 11 '11 at 08:39 AM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.