简体   繁体   中英

Start Date and End Date Query not working

Following is the Requirement

If the search date range is 5/15/2013 to 5/21/2013, then events with the following event date ranges would be included or excluded:

1/01/2013 to 6/30/2013 include
2/15/2013 to 3/15/2013 exclude
5/01/2013 to 5/19/2013 include
5/01/2013 to 5/14/2013 exclude
5/15/2013 to 5/15/2013 include
5/21/2013 to 5/21/2013 include
5/17/2013 to 5/30/2013 include
5/16/2013 to 5/20/2013 include
5/22/2013 to 5/30/2013 exclude
*5/16/2013 to 00/00/0000 include
5/22/2013 to 00/00/0000 exclude*

Below is the query which I made but its not following above conditions

SELECT cm_id,cm_date_from,cm_date_to FROM Cat_Master
WHERE TRUE  AND ( "2013-05-15" BETWEEN cm_date_from AND cm_date_to OR "2013-05-21" BETWEEN cm_date_from AND cm_date_to
    OR "2013-05-15" >= cm_date_from AND "2013-05-21" <= cm_date_to   OR "2013-05-15" <= cm_date_from AND "2013-05-21" >= cm_date_to
    )

Please guide on above query thanks

TO_DATE( string1, [ format_mask ], [ nls_language ] )

TO_DATE( '2013-05-15, 'yyyy-mm-dd' ).

For date comparisons use date variable.

PS: I asumed you are using Oracle Db (Since you did not mention)

It seems you missed braces after 2nd and 3rd OR operators.
Try this:

SELECT cm_id,cm_date_from,cm_date_to
FROM Cat_Master
WHERE TRUE  AND ( 
  "2013-05-15" BETWEEN cm_date_from AND cm_date_to
  OR
  "2013-05-21" BETWEEN cm_date_from AND cm_date_to
  OR
  ( "2013-05-15" >= cm_date_from AND "2013-05-21" <= cm_date_to )
  OR
  ( "2013-05-15" <= cm_date_from AND "2013-05-21" >= cm_date_to )
    )

Use date types to compare dates.

  WHERE ( to_date('2013-05-15','YYYY-MM-DD') BETWEEN cm_date_from AND cm_date_to OR 
  to_date('2013-05-21','YYYY-MM-DD') BETWEEN cm_date_from AND cm_date_to
  OR to_date('2013-05-15','YYYY-MM-DD') >= cm_date_from AND to_date('2013-05-21','YYYY-MM-DD') <= 
  cm_date_to   OR to_date('2013-05-15','YYYY-MM-DD') <= cm_date_from AND 
to_date('2013-05-21','YYYY-MM-DD') >= cm_date_to  );

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