locked
00 after decimal in sql RRS feed

  • Question

  • User-257070954 posted

    Hi,

    I had 4 integer and one decimal colum in sql table

    i try to add four int column and divided by 4 and save in decimal colum. but when save output in decimal colum getting after decimal 00 always?

    col1, col2, col3, col4, col5
    60, 100, 0 30 47.00 expected answer (60+100+0+30)/4 is 47.5
    how will get correct answer?

    Tuesday, March 20, 2018 4:11 AM

All replies

  • User364663285 posted

    Try

    select cast((60+100+0+30) as decimal(10,2))/4
    

    Tuesday, March 20, 2018 4:41 AM
  • User516094431 posted

    col1, col2, col3, col4, col5
    60, 100, 0 30 47.00 expected answer (60+100+0+30)/4 is 47.5
    how will get correct answer?

    As per you needs check following query. You can use this query according your requirement

    DECLARE @col1 AS INT = 60;
    DECLARE @col2 AS INT = 100;
    DECLARE @col3 AS INT = 0;
    DECLARE @col4 AS INT = 30;
    
    
    select cast(cast((@col1+@col2+@col3+@col4) as decimal(10,2))/4 as numeric(36,2)) AS col5;


    .

    Tuesday, March 20, 2018 5:24 AM
  • User1413134711 posted

    Hi You can use below query 

    select FORMAT(cast((60+100+0+30) as decimal(10,2))/4,'N2')

    Hope it will help you . 

    Wednesday, March 28, 2018 12:49 PM