You're talking about running SQL against a flat file? The only database I know of that can handle that is Oracle using "external" to make the outside file appear to be an internal table. Is that what you're using? If so, you can probably use a decode on the second date.
Something like:
Code:
select .... from external_table
order by decode(second_date, '0000-00-00', first_date,
second_date)
Decode is basically an "if" statement, so if you're not using Oracle, (and decode is not available) you can use whatever the language allows for conditionals. This may or may not be supported in the order by clause for other DBMSs, but Oracle does support it.
The decode above translates as:
if (second_date == '0000-00-00') {
return first_date;
} else {
return second_date;
}