SQL working

SQL is now working, may need changes to be standards compliant. Needs
date code fixing.