clear markdown compare help best fiddles feedback
clear markdown feedback
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 3601446 fiddles created (47986 in the last week).

CREATE TABLE foo_events AS select '2020-10-01'::date as ev_date, 'asdf' as ev_name union all select '2020-10-03' as ev_date, 'fdsa' as ev_name;
2 rows affected
 hidden batch(es)


/* Function approach */ CREATE FUNCTION get_foo_events_between(start_date date, end_date date) returns table(ev_date date, ev_name text) AS $$ SELECT * from foo_events WHERE ev_date >= $1 AND ev_date <= $2 $$ LANGUAGE SQL; /* Users use it this way */ select * from get_foo_events_between('2020-10-02', '2020-10-04');
ev_date ev_name
2020-10-03 fdsa
 hidden batch(es)


/* CTE approach. */ WITH user_defined_settings AS ( select /* Users edit these dates, then paste entire query */ '2020-10-02' AS start_date_str, '2020-10-04' AS end_date_str ), settings as (select start_date_str::date AS start_date, end_date_str::date AS end_date from user_defined_settings), actual_query as ( SELECT * from foo_events WHERE ev_date >= (select start_date from settings) AND ev_date <= (select end_date from settings) ) SELECT * from actual_query;
ev_date ev_name
2020-10-03 fdsa
 hidden batch(es)