SQL divide two integers and get a decimal value error

前端 未结 4 1434
刺人心
刺人心 2021-01-19 09:45

In an SQL statement, I am trying to divide two integers (integer 1 is \"abc\" in my code below, integer 2 is \"xyz\" in my code), and get a result as a decimal (def in my co

相关标签:
4条回答
  • 2021-01-19 10:29

    This should do it. Also add divide by zero check.

     SELECT CONVERT(DECIMAL(4,3), abc) / NULLIF(xyz,0)
    
    0 讨论(0)
  • 2021-01-19 10:41

    Hope this will work...

    SELECT CONVERT(DECIMAL(4,3), abc/10.0) AS def
      FROM dbo.whatever;
    
    0 讨论(0)
  • 2021-01-19 10:42

    Try something like

    SELECT CONVERT(DECIMAL(4,3), abc/(xyz * 1.0)) AS def
    
    0 讨论(0)
  • 2021-01-19 10:46

    Convert to decimal before the divide, not after. The convert for answer format.

    SELECT 
      CONVERT( DECIMAL(4,3)
             , ( CONVERT(DECIMAL(10,3), abc) / CONVERT(DECIMAL(10,3), xyz) ) 
             ) AS def
    
    0 讨论(0)
提交回复
热议问题