clear markdown compare help best fiddles feedback dbanow.uk
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 2591595 fiddles created (45746 in the last week).

CREATE TABLE tablename ( `due_date` DATE, `date_paid` VARCHAR(10), `paid_flag` INTEGER ); INSERT INTO tablename (`due_date`, `date_paid`, `paid_flag`) VALUES ('2020-09-28', NULL, '0'), ('2020-09-29', NULL, '0'), ('2020-10-01', NULL, '0'), ('2020-09-14', '2020-09-14', '1'), ('2020-09-29', '2020-09-29', '1'), ('2020-10-05', '2020-10-05', '1');
 hidden batch(es)


select * from tablename order by paid_flag, ((not paid_flag) - paid_flag) * to_seconds(due_date)
due_date date_paid paid_flag
2020-09-28 0
2020-09-29 0
2020-10-01 0
2020-10-05 2020-10-05 1
2020-09-29 2020-09-29 1
2020-09-14 2020-09-14 1
 hidden batch(es)