处理的是一个时间字段。当记录中有null的时候,返回null,否则返回最大时间。
例如:
SELECT     MAX(AddTime)  FROM   表 当addtime有null时返回null,否则返回MAX(AddTime)
我直接max(addtime)就算字段里有NULL它始终返回时间最大值。这种情况下该怎么处理?写函数?

解决方案 »

  1.   

    select 最大时间=case when IsNull((select count(*) from 表 where AddTime Is Null),0)>0 then null else max(AddTime) end
    from 表
      

  2.   


    declare @表 table (AddTime datetime)
    insert into @表
    select '2010-11-27' union all
    select '2010-11-26' union all
    select nulldeclare @i int
    select @i=count(1) from @表 where AddTime is null
    SELECT case when  @i > 0 then null else MAX(AddTime) end FROM @表