表t: 字段a int型, 字段b varchar(20)select a, b from t
where b is not null
and  ltrim(rtrim(b)) <> ''
and  isdate(b) = 1 
and  convert(datetime, b) > getdate() ;执行时提示:服务器: 消息 241,级别 16,状态 1,行 1
从字符串转换为 datetime 时发生语法错误。

解决方案 »

  1.   

    这个从逻辑上讲好像是对的,sqlserver执行基于成本的优化,实际上查询优化时,执行并非一定先isdate(b)=1过滤后才再执行convert这个条件。也就是说,where 后的条件不是顺序执行的。以前讨论过。
    改用case when解决select a,b from t
       where b is not null
          and
             getdate()<
            (case when isdate(b)=1 then
                  convert(datetime,b)
              else
                  '1900-1-1'  --当b不能转换为datetime时,即条件不符,给出一个getdate()不可能小于的值
              end
            )
    当然,也可以是用子查询
      

  2.   


    convert(datetime, b, "yyyy-mm-dd") > getdate() ; 
      

  3.   

    [Quote=引用 3 楼 fcuandy 的回复:]
    这个从逻辑上讲好像是对的,sqlserver执行基于成本的优化,实际上查询优化时,执行并非一定先isdate(b)=1过滤后才再执行convert这个条件。 也就是说,where 后的条件不是顺序执行的。以前讨论过。 
    请问 fcuandy:SQL的执行顺序是怎样的...?
    字段b有空,非datetime型...
      

  4.   


    select * 
    from b
    where case when isdate(b)=1 
               then case when convert(datetime,c2)>getdate() 
                    then 1 
                    else 0 
                    end 
               else 0 
          end = 1改成这样吧。
    你语句中的前两个条件是多余的,isdate()就已经足够了。
    如果SQL是按你语句的顺序去执行,那你的语句是正确的,
    但事实上,SQL是不会这样做,优化器会决定先执行哪一部分。