Fixed an issue with SQL returning the wrong index value.