简体   繁体   中英

How to code current date - to select rows where actual_date is = or > than the current_date?

In the following SQL for SQL Server 2014 (v12.00.6433), e.actual_date is a datetime Null column. The code shown results in an error:

Msg 156, Level 15, State 1, Line 13
Incorrect syntax near the keyword 'CURRENT_DATE'.

The purpose here is that we want to see all clients activity only for the past 2 and half years from the current date.

select distinct 
    ac.people_id, ac.first_name, ac.last_name, ac.id_no, 
    ad.city, ed.is_service_event, co.description as County,
    pe.program_name, pe.program_start_date, pe.program_end_date, 
    el.actual_date, el.approved_date, el.program_providing_service, 
    el.duration, ed.event_name
from
    all_clients_view ac
inner join 
    address ad on ad.people_id = ac.people_id
left join 
    county co on ad.county = co.county_id
inner join 
    program_enrollment_expanded_view pe on pe.people_id = ac.people_id
inner join 
    event_log el on ac.people_id = el.people_id
inner join 
    event_definition ed on el.event_definition_id = ed.event_definition_id
where 
    el.actual_date > CURRENT_DATE() - (365 * 2 + 366 / 2) 

CURRENT_DATE() is not a sql server function. Try replacing it with GETDATE() .

Going two and a half years back can also be done by substracting 30 months like this: DATEADD(month, -30, GETDATE())

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