Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Oracle PL/SQL example of a query with an optional date range
/* Oracle PL/SQL example of a query with an optional date range */
SET SERVEROUTPUT ON;
DECLARE
StartDate DATE := NULL;
EndDate DATE := NULL;
--StartDate DATE := TO_DATE ('01/01/2017', 'mm/dd/yyyy');
--EndDate DATE := TO_DATE ('02/01/2017', 'mm/dd/yyyy');
RecordCount NUMBER;
BEGIN
SELECT COUNT(*)
INTO RecordCount
FROM TABLE_NAME t
WHERE (StartDate IS NULL OR t.DATE_COLUMN >= StartDate)
AND (EndDate IS NULL OR t.DATE_COLUMN <= EndDate);
DBMS_OUTPUT.PUT_LINE(RecordCount);
END;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment