DECLARE my_cursor CURSOR FOR SELECT
column1,
column2,
non_y2k_char_date_column,
case
when substr(non_y2k_char_date_column,5,2) > char(year(current_date)) then '19' ||
substr(non_y2k_char_date_column,5,2) ||
substr(non_y2k_char_date_column,3,2) ||
substr(non_y2k_char_date_column,1,2)
else '20' ||
substr(non_y2k_char_date_column,5,2) ||
substr(non_y2k_char_date_column,3,2) ||
substr(non_y2k_char_date_column,1,2)
end as sortable_date,
column4
from your_table
where ...
order by sortable_date
Convert the ddmmyy to a true date with this expression:
DATE( TRIM (CHAR(mo)) || ‘/’ ||
TRIM (CHAR(da)) || ‘/’ ||
TRIM (CHAR((cn * 100) + yr)))
Then sort on the column number.
If you have potential invalid dates, you can use the same expression to set up a case statement with a valid range of dates and default substitution when invalid.
Gary
I should add than if you do not have “cn”, you will also have to handle that with case/range/default logic.
Thanks for the reply, can you explain it a bit more?
Thanks for the reply, however i found another solution:
DECLARE my_cursor CURSOR FOR SELECT
column1,
column2,
non_y2k_char_date_column,
case substr(non_y2k_char_date_column,5,2)
when > char(year(current_date)) then ’19′ ||
substr(non_y2k_char_date_column,5,2) ||
substr(non_y2k_char_date_column,3,2) ||
substr(non_y2k_char_date_column,1,2)
else ’20′ ||
substr(non_y2k_char_date_column,5,2) ||
substr(non_y2k_char_date_column,3,2) ||
substr(non_y2k_char_date_column,1,2)
end as sortable_date,
column4
from your_table
where …
order by sortable_date