Returns 0 don't know why

  • why does this return 0 ???

    "select cast((30/364) as dec(20,20))"

    Steve Johnson


    Steve Johnson

  • It thinks that the numbers are int's to start with. Try to do:

    select cast((30.0/364.0) as dec(20,20))

    That will return the value.

    Tom Goltl

  • thanks.....

    Steve Johnson


    Steve Johnson

  • Hi,

    I reckon, it picks up everything as int. atleast one of the values should be casted to the final casting.

    ie., the final cast is dec(20,20). So, either 30 or 364 should be casted as dec(n,m)

    This is just a suggestion.

  • Microsoft uses INT as the cast of a number that has no special characters and for some reason they decide if it starts int then the results of math should be the same output, even if overridden. This also happens in C.

  • Right. If there's no decimal point, SQL Server will assume the constant to be an integer. Since integer divison should not result in a decimal part (violation of the type), you'll get back only an integer. And this division is occuring before the CAST statement, which means you enter the CAST with a 0. Hence the need to specify 30.0 and 364.0 instead.

    K. Brian Kelley

    bkelley@sqlservercentral.com

    http://www.sqlservercentral.com/columnists/bkelley/

    K. Brian Kelley
    @kbriankelley

  • Thanks again for all the info.... It has been very useful.

    Steve Johnson


    Steve Johnson

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply