Dividing in a SELECT statement

  • I am trying to divide two columns from two tables in the following query:

    SELECT Stage2file.newdist AS newdist,

    CAST(distdlrcnt/distcnt as decimal(10,2)) AS Prop_DD

    INTO dbo.rdddf2

    FROM dbo.Stage2file

    join

    dbo.Stage3file

    ON Stage2file.newdist = Stage3file.newdist

    However when I run this although the column Prop_DD is a decimal it only seems to contain integer values, so say there is a value that should be 0.4 it is output as 0. Is it becasue the two fields I am dividing are both ints? If not why is this happening and how do I solve the problem. It seems a really simple problem, but I just can't get around it. Any help would be much appreciated,

    thanks,

    Matt

  • If both fields are int then the result would be a int.

    SELECT Stage2file.newdist AS newdist,

    CAST(convert(decimal(10,2),distdlrcnt))/distcnt AS Prop_DD

    INTO dbo.rdddf2

    FROM dbo.Stage2file

    join

    dbo.Stage3file

    ON Stage2file.newdist = Stage3file.newdist

    Jeremy

  • Thanks a lot. I tried your code and it worked after removing the CAST function from the code (it didn't seem to be needed). I'm fairly new to this so learning all the subtleties and functions of SQL server 2000 means that I am hitting a few problems right now.

Viewing 3 posts - 1 through 2 (of 2 total)

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