Converting Access Code to SQL

前端 未结 2 1136
鱼传尺愫
鱼传尺愫 2021-01-29 13:49

I have the following code in Access and I need it to work in sql, the Is numeric portion is throwing me off.

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


        
相关标签:
2条回答
  • 2021-01-29 14:12

    You will replace the IIF() with a CASE expression. The IsNumeric() is valid in SQL Server:

    Sum([Employee Count]*
        case when IsNumeric([Length]) = 1
              then [Length]
              else 0 end) AS Total_hours,
    
    0 讨论(0)
  • 2021-01-29 14:30

    You can also just filter "not isnumeric" out since 0 will not impact SUM aggregation.

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

    See code below.

    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

    0 讨论(0)
提交回复
热议问题