SQL Cast from int to decimal SQL Cast from int to decimal sql sql

SQL Cast from int to decimal


Write it like this:

SELECT CAST(CAST(Total As float) / TotalAnswers * 100 As decimal(8, 2))


DECIMAL(2) is a decimal of 2 digits before and no digits after the decimal point. That doesn't allow you to have digits after the decimal point!

The DECIMAL(p,s) specification defines the number of total digits (p) and the number of digits after the decimal point (s) (where s cannot be larger than p and is 0 if ommitted).

You need to use something like DECIMAL(4,2) (4 digit total - 2 of which after the decimal point; so therefore: also 2 before the decimal point) or something like that - some digits before and some after the decimal point - then you'll see your desired result!

For details on the DECIMAL type and its syntax, consult the MSDN Books Online topic on decimal and numeric


There are actual a couple of parameters..

(p, s)

where

  • p represents the "precision" - the total number of digits before and after the decimal point.
  • s represents the "scale" - the number of digits after the decimal point.

By only giving 1 parameter, you are giving the precision only.

See MDSN.