In SAP ABAP, to select records between two dates in an Open SQL SELECT statement, you can use the BETWEEN operator or a combination of >= and <=.


1️⃣ Using BETWEEN

DATA: lt_data TYPE TABLE OF ztable, " Replace 'ztable' with your actual table name
lv_date_from TYPE sy-datum VALUE '20240301',
lv_date_to TYPE sy-datum VALUE '20240331'.
SELECT * FROM ztable
WHERE datum BETWEEN lv_date_from AND lv_date_to
INTO TABLE lt_data.
IF sy-subrc = 0.
WRITE: 'Records found:', LINES( lt_data ).
ELSE.
WRITE: 'No records found.'.
ENDIF.

2️⃣ Using >= and <=

SELECT * FROM ztable
WHERE datum >= lv_date_from
AND datum <= lv_date_to
INTO TABLE lt_data.

This is functionally the same as BETWEEN but explicitly specifies both conditions.


3️⃣ Using Dynamic Date Ranges (e.g., Last 30 Days)

DATA: lv_today TYPE sy-datum,
lv_past_30 TYPE sy-datum.
lv_today = sy-datum.
lv_past_30 = sy-datum - 30.
SELECT * FROM ztable
WHERE datum BETWEEN lv_past_30 AND lv_today
INTO TABLE lt_data.

This retrieves data for the last 30 days dynamically.

Would you like help with performance optimization, like using an index or buffering? 🚀