value只有数字吗?如果有字符串那可不行
如果是数字,月份不能大于12,日期不能大于31
你看表里的数据是否合法?

解决方案 »

  1.   

    Left(cast([Value]as int),4) + '-' + Left(Right(cast([Value]as int),4),2) + '-' + Right(cast([Value]as int),2)
    不用转换直接
    datediff(day,forecastdate,[Value) 即可
    当然 我觉得你的 Value值可能包含不能转换成日期格式的数据 比如 20090229 或者 20091301
      

  2.   

    Try :
      直接
    select datediff(day,forecastdate,cast([Value] as datetime))
      

  3.   

    说明:(1) 字段 value 是数字;forecastdate是日期;
    (2)Left(cast([Value]as int),4) + '-' + Left(Right(cast([Value]as int),4),2) + '-' + Right(cast([Value]as int),2)
      这个得出的是这样的结果: 
    2007-06-02
    2006-12-21
    2007-03-23
    2007-03-30------------------
    现在的问题是:再加datediff()函数就有问题....报错:
    错误信息: Conversion failed when converting date and/or time from character string. 
      

  4.   

    (1) value 是 SmallDecimal数据类型;数字,如:
    67512000.0000
    4204000.0000(2)forecastdate是日期,如:
    2007-01-31 00:00:00
    2004-03-12 00:00:00(3)Left(cast([Value]as int),4) + '-' + Left(Right(cast([Value]as int),4),2) + '-' + Right(cast([Value]as int),2)
      这个得出的是这样的结果:
    2007-06-02
    2006-12-21
    2007-03-23
    2007-03-30 (4) 就是再往外扩:datediff()函数就报错了:
    语句执行报错:datediff(day,forecastdate,Left(cast([Value]as int),4) + '-' + Left(Right(cast([Value]as int),4),2) + '-' + Right(cast([Value]as int),2))===================================================