-2

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

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

2 回答 2

2

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

Sum([Employee Count]*
    case when IsNumeric([Length]) = 1
          then [Length]
          else 0 end) AS Total_hours,
于 2013-01-21T20:20:15.577 回答
0

您也可以只过滤掉“非数字”,因为 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

于 2013-01-21T20:32:59.493 回答