def find_prev_keyword(sql, n_skip=0):
 if (not sql.strip()):
  return (None, '')
 parsed = sqlparse.parse(sql)[0]
 flattened = list(parsed.flatten())
 flattened = flattened[:(len(flattened) - n_skip)]
 logical_operators = ('AND', 'OR', 'NOT', 'BETWEEN')
 for t in reversed(flattened):
  if ((t.value == '(') or (t.is_keyword and (t.value.upper() not in logical_operators))):
   idx = flattened.index(t)
   text = ''.join((tok.value for tok in flattened[:(idx + 1)]))
   return (t, text)
 return (None, '')

def find_prev_keyword ( sql , n_skip = 0 ) : if ( not sql . strip ( ) ) : return ( None , '' ) parsed = sqlparse . parse ( sql ) [ 0 ] flattened = list ( parsed . flatten ( ) ) flattened = flattened [ : ( len ( flattened ) - n_skip ) ] logical_operators = ( 'AND' , 'OR' , 'NOT' , 'BETWEEN' ) for t in reversed ( flattened ) : if ( ( t . value == '(' ) or ( t . is_keyword and ( t . value . upper ( ) not in logical_operators ) ) ) : idx = flattened . index ( t ) text = '' . join ( ( tok . value for tok in flattened [ : ( idx + 1 ) ] ) ) return ( t , text ) return ( None , '' )
find the last sql keyword in an sql statement returns the value of the last keyword .