开发者

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

Please help me to find out error in my SQL query. I have created this query to compare dates

select * from Joinplans jp
where cast(convert(v开发者_运维问答archar,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'

I am using SQL Server 2005.


You wrote case instead of cast in two instances.


If planStartDate is actually a date, then there is no need to cast it to a character column:

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:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜