Skip to content

Instantly share code, notes, and snippets.

@randyburden
Created November 6, 2017 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randyburden/08c1698bc180e98b9247a165dead7d2e to your computer and use it in GitHub Desktop.
Save randyburden/08c1698bc180e98b9247a165dead7d2e to your computer and use it in GitHub Desktop.
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