如何为SQL Server重新编写此MS Access表达式

时间:2013-01-22 17:25:22

标签: sql sql-server sql-server-2008

我是网站的新用户,我有一个问题,我在Access窗体中有一些代码,我在SQL Server 2008中重做,并且它有SUM,IIF和IsNumeric都在一行中,我很困惑如何在SQL中重写它。

Sum([Employee COUNT]*IIf(IsNull([2011]),0,CDbl([2011]))*IIf(IsNumeric([Length]),CDbl([Length]),0)) AS Cost

2 个答案:

答案 0 :(得分:6)

我相信这会做你需要的:

Sum([Employee COUNT]
    * (case 
            when [2011] Is Null 
            then 0 
            else cast([2011] as float) 
        end)
    * (case 
            when IsNumeric([Length])= 1 
            then cast([Length] as float) 
            else 0 
        end)) AS Cost

答案 1 :(得分:0)

SUM([Employee Count] 
  * COALESCE(
      [2011],
       CASE WHEN 
         ISNUMERIC([2011]) = 1 
       THEN 
         CAST([2011] AS DECIMAL(18,4) 
       ELSE 
         0 
       END
    )
  * CASE WHEN 
      ISNUMERIC([Length]) = 1
    THEN 
      -- choose your own precision here.
      CAST ([Length] AS DECIMAL(18,4))
    ELSE
      0
    END
) AS Cost