简体   繁体   English

请告诉我我的日期比较sql查询中的错误是什么

[英]Please tell me what is error in my date comparison sql query

Please help me to find out error in my SQL query. 请帮我查一下我的SQL查询中的错误。 I have created this query to compare dates 我创建了此查询来比较日期

select * from Joinplans jp
where cast(convert(varchar,GETDATE(),103) AS  datetime) BETWEEN    
    CASE(convert(varchar,jp.planstartDate,103) AS datetime) AND
    CASE(convert(varchar,DATEADD(DAY,jp.planDays,jp.planstartDate),103) AS DATETIME)

It's giving me the error: 它给了我错误:

incorrect near 'AS' 'AS'附近不正确

I am using SQL Server 2005. 我正在使用SQL Server 2005。

你在两个实例中编写了case而不是cast

If planStartDate is actually a date, then there is no need to cast it to a character column: 如果planStartDate实际上是一个日期,则无需将其强制转换为字符列:

Select ...
From Joinplans jp 
where GetDate() Between planStartDate And DateAdd(day, jp.planDays, jp.planStartDate)

Now, if planStartDate is storing both date and time data, then you might want to use something like: 现在,如果planStartDate存储日期和时间数据,那么您可能希望使用以下内容:

Select ...
From Joinplans jp 
Where planStartDate <= GetDate()  
    And GetDate() < DateAdd(day, jp.planDays + 1, jp.planStartDate)

This ensures that all times on the last date calculated via the DateAdd function are included 这可确保包含通过DateAdd函数计算的最后日期的所有时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM