Changed the if expression for selecting the correct piece of SQL to use.