将访问代码转换为SQL

时间:2013-01-21 20:18:12

标签: sql ms-access

我在Access中有以下代码,我需要它在sql中工作,Is数字部分让我失望。

Sum([Employee Count]*IIf(IsNumeric([Length]),[Length],0)) AS Total_hours, 

2 个答案:

答案 0 :(得分:2)

您将IIF()替换为CASE表达式。 IsNumeric()在SQL Server中有效:

Sum([Employee Count]*
    case when IsNumeric([Length]) = 1
          then [Length]
          else 0 end) AS Total_hours,

答案 1 :(得分:0)

您也可以只过滤“not isnumeric”,因为0不会影响SUM聚合。

select
Sum([Employee Count]*[Length]) AS Total_hours
...
where isnumeric([Length]) = 1

见下面的代码。

declare @table table (abc varchar(100))
insert into @table
select '1'
union select '200'
union select '200A'

select sum(convert(int,abc))
from @table
where isnumeric(abc) = 1

sqlfiddle