How do I use a sub-string of a field? How do I convert SELECT title , SUBSTRING(title,13,4) as num FROM node_field_data ORDER BY ABS(num) to the format for Drupal 8?

I tried this code.

 $query = $db-&gt;select('node_field_data', 'nfd')
  -&gt;extend('\Drupal\Core\Database\Query\TableSortExtender')
  -&gt;extend('\Drupal\Core\Database\Query\PagerSelectExtender');
  $query-&gt;limit('50');
  $query-&gt;fields('nfd',array('title',substring(title,13,4) as num,'changed','nid'));
 -&gt;orderBy('num', 'ASC')
You need to add an expression with SelectInterface::addExpression:

$query = $db-&gt;select('node_field_data', 'nfd')
  -&gt;fields('nfd', ['title'])
  -&gt;extend(\Drupal\Core\Database\Query\TableSortExtender::class)
  -&gt;extend(\Drupal\Core\Database\Query\PagerSelectExtender::class);

$expr_alias = $query-&gt;addExpression('SUBSTRING(title, 13, 4)', 'num');

$query-&gt;orderBy($expr_alias, 'ASC')-&gt;limit('50');