diff --git a/application/Espo/ORM/DB/Query/Base.php b/application/Espo/ORM/DB/Query/Base.php
index <HASH>..<HASH> 100644
--- a/application/Espo/ORM/DB/Query/Base.php
+++ b/application/Espo/ORM/DB/Query/Base.php
@@ -1547,7 +1547,9 @@ abstract class Base
 
     public function sanitizeSelectAlias($string)
     {
-        return preg_replace('/[^A-Za-z0-9_:\'" .,\-\(\)]+/', '', $string);
+        $string = preg_replace('/[^A-Za-z\r\n0-9_:\'" .,\-\(\)]+/', '', $string);
+        if (strlen($string) > 256) $string = substr($string, 0, 256);
+        return $string;
     }
 
     public function sanitizeSelectItem($string)
orm sanitizeSelectAlias fix