select * from (select
CAST(('2010-'+[PartDate]) as datetime) [NewPartDate]
from V_HouseAndRateDetail where testTypes='2') as temp 
where [NewPartDate]>GETDATE()
解释下 视图里的 PartDate是月日的形式 由于存储时可能是12-1这种形式而正常的数据库日期是12-01 所以在直接跟日期比较时会出现范围不准确的问题,才有上边的CAST转换,不加where查询是没问题的,但是在进行where查询时就会出现“从字符串转换日期和/或时间时,转换失败。” 期间尝试过转换成varchar比较也是同样的错误,望哪位大侠指教下。

解决方案 »

  1.   

    cast(newpartdate as datatime)
      

  2.   

    select * from (select
    CAST(('2010-'+[PartDate]) as datetime) [NewPartDate]
    from V_HouseAndRateDetail where testTypes='2') as temp 
    where '2010-'+[PartDate]>GETDATE()
      

  3.   

    select * from 
    (
    select CAST(('2010-'+[PartDate]) as datetime) [NewPartDate]
    from V_HouseAndRateDetail where testTypes='2'
    ) as temp 
    where isdate([NewPartDate])=0看看上面结果
      

  4.   

    直接在里面加条件试试select
    CAST(('2010-'+[PartDate]) as datetime) [NewPartDate]
    from V_HouseAndRateDetail
    where testTypes='2' 
    and '2010-'+[PartDate]>GETDATE()
      

  5.   

    select * from (select
    CAST(('2010-'+[PartDate]) as datetime) [NewPartDate]
    from V_HouseAndRateDetail where testTypes='2') as temp 
    where CAST(('2010-'+[PartDate]) as datetime)>GETDATE()
      

  6.   

    上边均已测试,问题在于大家的想法都差不多,但是纠结的是只要加上where条件就出错
      

  7.   

    where后面不可以直接使用别名的,别名可以在orderby中使用。
      

  8.   

    什么意思?没明白,如果不用别名我的where过滤条件不就没办法实现了吗?
      

  9.   

    好的多谢,以前没这么写过 如9楼所说的确是不能用的 随便虚拟了个字段用like 查询页报同样的错误
      

  10.   

    汗一个 貌似这个语句出的问题还是在时间上select * from (
    select 'aaa' aaa from V_HouseAndRateDetail 
    where (testTypes='2') 
    ) as temp where aaa like '%aa%'
    这个语句可以正常执行
      

  11.   

    select [newPaymentStartDate] from 
    (select [newPartDate]=case when cast(('2010-'+[PartDate]) as datetime) 
    between DATEADD(mm,  DATEDIFF(mm,0,'2010-12-1'),  0)
    and dateadd(ms,-3,DATEADD(mm,  DATEDIFF(m,0,'2010-12-1')+1, 0)) 
    then 0
    else 1 end
    from V_HouseAndRateDetail where (testTypes='2') ) 
    as temp where newPartDate=0
    连这样都出错纠结啊