Fixed wrapping problems with sql query