Magento 1.9.2.0: table "sales_flat_order_grid" contains extra space in customer's name value
In Magento 1.9.2 the middle name has been added to this column:
Source: https://github.com/OpenMage/magento-mirror/blob/magento-1.9/app/code/core/Mage/Sales/Model/Resource/Order.php#L93-L99
$ifnullFirst = $adapter->getIfNullSql('{{table}}.firstname', $adapter->quote(''));
$ifnullMiddle = $adapter->getIfNullSql('{{table}}.middlename', $adapter->quote(''));
$ifnullLast = $adapter->getIfNullSql('{{table}}.lastname', $adapter->quote(''));
$concatAddress = $adapter->getConcatSql(array(
$ifnullFirst,
$adapter->quote(' '),
$ifnullMiddle,
$adapter->quote(' '),
$ifnullLast
));
Unfortunately they did not really think about the case where a customer does not have a middle name. This is how the code should look like:
$ifnullFirst = $adapter->getIfNullSql('{{table}}.firstname', $adapter->quote(''));
$ifnullMiddle = $adapter->getIfNullSql('{{table}}.middlename', $adapter->quote(''));
$ifnullLast = $adapter->getIfNullSql('{{table}}.lastname', $adapter->quote(''));
$concatAddress = $adapter->getConcatSql(array(
$ifnullFirst,
$adapter->quote(' '),
$ifnullMiddle,
new Zend_Db_Expr('IF({{table}}.middlename IS NULL OR {{table}}.middlename="", "", " ")'),
$ifnullLast
));
You can copy the file to app/code/local/Mage/Sales/Model/Resource/Order.php
and patch it as described.
To fix the existing records, you can use this throwaway PHP script:
<?php
require 'app/Mage.php';
Mage::app();
Mage::getModel('sales/order')->getResource()->updateGridRecords(
Mage::getResourceModel('sales/order_collection')->getAllIds());
echo 'done';
Place it as fixordergrid.php
in the Magento root directory, execute and delete it. It might take some time, so you should better run it from the console, not in the browser:
php fixordergrid.php
To further the accepted answer, it's best practice not to edit the core magento code so using a rewrite with the fix would be better.
In config.xml
<global>
<models>
<sales_resource>
<rewrite>
<order>Yournamespace_Yourextension_Model_Sales_Order_Resource_Order</order>
</rewrite>
</sales_resource>
</models>
</global>