Fixed sql and date formatting