将nvarchar转换为int时转换失败

时间:2016-03-07 17:51:29

标签: sql-server-2008

我遇到上述错误,任何人都可以帮助解决代码问题...

P.YKey BETWEEN YEAR('''+ CONVERT(VARCHAR(50), @BDate, 103) +''') 
           AND YEAR('''+ CONVERT(VARCHAR(50), @eDate, 103) +''')
   AND P.DCreation BETWEEN '''+ CONVERT(VARCHAR(50), @BDate, 103) +''' 
                       AND '''+ CONVERT(VARCHAR(50), @eDate, 103) +''''

2 个答案:

答案 0 :(得分:0)

@BDate的类型是什么,@ eDate?

更新: 由于两个变量的类型是DATETIME,因此无需进行转换。

P.YKey BETWEEN YEAR(@BDate) 
           AND YEAR(@eDate)
   AND P.DCreation BETWEEN @BDate 
                       AND @eDate

答案 1 :(得分:0)

因为您似乎只对创建日期感兴趣,而不是时间,我还会将日期时间转换为日期。因此,假设DCreation也是日期时间:

P.YKey BETWEEN YEAR(@BDate) 
           AND YEAR(@eDate)
   AND cast(P.DCreation as date) BETWEEN cast(@BDate as date) 
                                 AND cast(@eDate as date)