fix sql function for string yyyyMMdd substring out of range