简体   繁体   中英

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(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'

I am using SQL Server 2005.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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