Treating string field as numeric to use as an exposed filter I have a field in my users' profiles that has been defined as varchar initially even though it holds numeric values. I'm trying to use that field as an exposed filter and perform "greater than or equal" operation on it; however, since it's a varchar field, View doesn't give me that option. 

In my module, I have come up with these two hooks to do a workaround: 

function MY_MODULE_views_query_alter(&amp;$view, &amp;$query) {
    if ( $view-&gt;name == 'manage_users' ) {
        foreach($query-&gt;where[1]['conditions'] as $key =&gt; $condition) {
            if ( $condition['field'] == 'field_data_field_count.field_count_value' ) {
                $query-&gt;where[1]['conditions'][$key]['value'] = (int)$query-&gt;where[1]['conditions'][$key]['value']; 
                $query-&gt;where[1]['conditions'][$key]['operator'] = '&gt;=';
                break; 
            }
        }
    }
}

function MY_MODULE_views_data_alter(&amp;$data) {
    $data['field_data_field_count']['field_count_value']['filter']['handler'] = 'views_handler_filter_numeric';  
    $data['field_data_field_count']['field_count_format']['filter']['handler'] = 'views_handler_filter_numeric';
    $data['field_data_field_count']['field_count_value']['argument']['handler'] = 'views_handler_filter_numeric';  
    $data['field_data_field_count']['field_count_format']['argument']['handler'] = 'views_handler_filter_numeric';
}


The first hook seems to be working fine and successfully changing the operator from LIKE to &gt;=; however, that's not enough for me because it still thinks 2 is greater than 15. So I added the second hook to make sure the field is actually treated as numeric, but that doesn't seem to have any effect on how the results are returned and it still thinks 2 is greater than 15.
Excellent question!
You only need the hook_views_query_alter function, here is what it should look like:

function MY_MODULE_views_query_alter(&amp;$view, &amp;$query) {
  if ( $view-&gt;name == 'manage_users' ) {

    // For filtering
    foreach($query-&gt;where[1]['conditions'] as $key =&gt; $condition) {
      if ( $condition['field'] == 'field_data_field_count.field_count_value' ) {
        $query-&gt;where[1]['conditions'][$key]['value'] = array(':val' =&gt; (int)$query-&gt;where[1]['conditions'][$key]['value']); 
        $query-&gt;where[1]['conditions'][$key]['field'] = 'CAST(' . $query-&gt;where[1]['conditions'][$key]['field'] . ' AS UNSIGNED) &gt;= :val';
        $query-&gt;where[1]['conditions'][$key]['operator'] = 'formula';
        break; 
      }
    }

    // For sorting as well
    foreach($query-&gt;orderby as $key =&gt; $condition) {
      if ( $condition['field'] == 'field_data_field_count_field_count_value' ) {                
        $query-&gt;orderby[$key]['field'] = 'CAST(' . $query-&gt;orderby[$key]['field'] . ' AS UNSIGNED)'; 
        break; 
      }
    }
  }
}


Most important parts in this code snippet:


Using CAST to change the varchar into an integer [doc]
Using the 'formula' operator to instruct views that you are using a complex SQL expression, check the source code of add_where_expression!  [doc]


Good luck!



Update Note: Even though this is working fine, the more elegant/readable way would be to have a function (equivalent to add_where_expression) that would allow overwriting a where/having condition.