bvstone

Converting a MMDDYY date format to YYMMDD for Sorting Using SQL

Posted:

Converting a MMDDYY date format to YYMMDD for Sorting Using SQL

I recently ran across a customers application that was listing items by a date, but for some reason the items were not in date order.

I looked into the file and the dates were stored as numeric, 6,0 as MMDDYY.  Not very handy for sorting.

In order to update the application I removed the RLA using READE and replaced it with SQL.  The SQL looked like the following:

SELECT                                            
  case                                                                  
  when DATE >= 100000                                                  
  then                                                                  
    (substr(DATE,5,2) || substr(DATE,1,2) || substr(DATE,3,2))         
  else                                                                  
   (substr(DATE,4,2) || '0' || substr(DATE,1,1) || substr(DATE,2,2))  
  end                                                                   
  as NEWDATE
FROM FILE

The case statement was needed because dates prior to October 1st are only 5 digits.

When this is done you have the NEWDATE field that you can use for sorting so that the data will be in the order intended.

Yes, this was an older V5R4 system.  Neither Y2K compliant or dates stored properly, but is that really very surprising?  :)





Reply




© Copyright 1983-2024 BVSTools
GreenBoard(v3) Powered by the eRPG SDK, MAILTOOL Plus!, GreenTools for Google Apps, jQuery, jQuery UI, BlockUI, CKEditor and running on the IBM i (AKA AS/400, iSeries, System i).