title: MySQL order by ABS if numeric
body: <p>I need to order some information with order by in a MySQL query. Is there a possibility to use  the ABS function only if the first char is numeric?</p>
  
  <p>Pseudo example:</p>
  
  <pre><code>SELECT * FROM table 
  ORDER BY
  (( name[0] = numeric ) ? ABS(name) : name)
  ASC
  </code></pre>
  
  <hr>
  
  <p>Thanks for the answers so far, tried a lot with your answers but I don't get the result I want.</p>
  
  <p>The current code:</p>
  
  <pre><code>&lt;?php
  $filters = $this-&gt;db-&gt;query("
      SELECT
          tb_spec_feature.feature_id,
          tb_spec_feature.name
      FROM
          tb_spec_feature
      WHERE
          filter=1
  ");
  $filters = $filters-&gt;result_array();
  foreach($filters as $key=&gt;$filter)
  {
      $filters_sub = $this-&gt;db-&gt;query("
          SELECT DISTINCT
              tb_spec_value.value_id,
              tb_spec_value.name as value_name,
              tb_spec_sign.name as sign_name
          FROM
              tb_spec_fk
          INNER JOIN
              tb_spec_value
          ON
              tb_spec_value.value_id = tb_spec_fk.value
          LEFT JOIN
              tb_spec_sign
          ON
              tb_spec_sign.sign_id = tb_spec_fk.sign
          WHERE
              feature=".$this-&gt;db-&gt;escape($filter['feature_id'])."
          ORDER BY
              ABS(tb_spec_value.name) ASC
      ");
      $filters[$key]['values'] = $filters_sub-&gt;result_array();
  }
  ?&gt;
  </code></pre>
  
  <p>This will output (tb_spec_feature.name and then the tb_spec_value.name):</p>
  
  <p><strong>Screen inches</strong></p>
  
  <ul>
  <li>7</li>
  <li>7.7</li>
  <li>7.9</li>
  <li>8.9</li>
  <li>9.7</li>
  <li>10</li>
  <li>10.1</li>
  <li>12.1</li>
  </ul>
  
  <p><strong>Resolution</strong></p>
  
  <ul>
  <li>800 x 480</li>
  <li>1024 x 600</li>
  <li>1024 x 768</li>
  <li>1280 x 800</li>
  <li>1280 x 768</li>
  <li>1920 x 1200</li>
  <li>2048 x 1536</li>
  </ul>
  
  <p><strong>Operating system</strong></p>
  
  <ul>
  <li>Android 3.1</li>
  <li>Android</li>
  <li>BlackBerry Tablet OS</li>
  <li>Android 3.2</li>
  <li>Android 2.3</li>
  <li>Android 4.0</li>
  <li>iOS 5</li>
  <li>Android 4.0.3</li>
  <li>iOS 4</li>
  <li>Android 2.2</li>
  <li>Windows 7 Professional</li>
  <li>Windows 8</li>
  <li>Windows 8 Pro</li>
  <li>iOS 6</li>
  </ul>
  
  <p>As you can see the inches and resolutions are okay, but the operating systems aren't ordered well. If I remove the ABS the result will be:</p>
  
  <p><strong>Screen inches</strong></p>
  
  <ul>
  <li>10</li>
  <li>10.1</li>
  <li>12.1</li>
  <li>7</li>
  <li>7.7</li>
  <li>7.9</li>
  <li>8.9</li>
  <li>9.7</li>
  </ul>
  
  <p><strong>Resolution</strong></p>
  
  <ul>
  <li>1024 x 600</li>
  <li>1024 x 768</li>
  <li>1280 x 768</li>
  <li>1280 x 800</li>
  <li>1366 x 768</li>
  <li>1920 x 1200</li>
  <li>2048 x 1536</li>
  <li>800 x 480</li>
  </ul>
  
  <p><strong>Operating system</strong></p>
  
  <ul>
  <li>Android</li>
  <li>Android 2.2</li>
  <li>Android 2.3</li>
  <li>Android 3.1</li>
  <li>Android 3.2</li>
  <li>Android 4.0</li>
  <li>Android 4.0.3</li>
  <li>BlackBerry Tablet OS</li>
  <li>iOS 4</li>
  <li>iOS 5</li>
  <li>iOS 6</li>
  <li>Windows 7 Professional</li>
  <li>Windows 8</li>
  <li>Windows 8 Pro</li>
  </ul>
  
  <p>As you can see now, the operating systems are ordered well but the inches and resolutions not.. I hope somebody can help me out :)</p>