working sql conversion