clean the legacy sql